Skip to content
Advertisement

String Aggregtion to create pivot columns in Big Query

I am trying to implement this exact solution linked below, but it seems GROUP_CONCAT_UNQUOTED is no longer a valid BQ function. Is there a solution to this that works in 2020?

p.s. I would have just commented on the original post, but apparently my reputation isn’t high enough yet…

Transpose rows into columns in BigQuery (Pivot implementation)

Advertisement

Answer

Below example is for BigQuery Standard SQL

Assume you have data as below

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 1 id, 'channel_title' key, 'Mahendra Guru' value UNION ALL
  SELECT 1, 'youtube_id', 'ugEGMG4-MdA' UNION ALL
  SELECT 1, 'channel_id', 'UCiDKcjKocimAO1tV' UNION ALL
  SELECT 1, 'examId', '72975611-4a5e-11e5' UNION ALL
  SELECT 1, 'postId', '1189e340-b08f' UNION ALL

  SELECT 2, 'channel_title', 'Ab Live' UNION ALL
  SELECT 2, 'youtube_id', '3TNbtTwLY0U' UNION ALL
  SELECT 2, 'channel_id', 'UCODeKM_D6JLf8jJt' UNION ALL
  SELECT 2, 'examId', '72975611-4a5e-11e5' UNION ALL
  SELECT 2, 'postId', '0c3e6590-afeb' 
)   

to get those keys pivoted – you can use below query

#standardSQL
SELECT id, 
  MAX(IF(key = "channel_id", value, NULL)) AS `channel_id`,
  MAX(IF(key = "channel_title", value, NULL)) AS `channel_title`,
  MAX(IF(key = "examId", value, NULL)) AS `examId`,
  MAX(IF(key = "postId", value, NULL)) AS `postId`,
  MAX(IF(key = "youtube_id", value, NULL)) AS `youtube_id` 
FROM `project.dataset.table` 
GROUP BY id 

with result

Row id  channel_id          channel_title   examId              postId          youtube_id   
1   1   UCiDKcjKocimAO1tV   Mahendra Guru   72975611-4a5e-11e5  1189e340-b08f   ugEGMG4-MdA  
2   2   UCODeKM_D6JLf8jJt   Ab Live         72975611-4a5e-11e5  0c3e6590-afeb   3TNbtTwLY0U     

to make your life simpler and not to type all those line for each and every key – you can instead generate the whole SQL text using below query

#standardSQL
SELECT 'SELECT id, ' || 
   STRING_AGG(
      'MAX(IF(key = "' || key || '", value, NULL)) as `' || key || '`'
   ) 
   || ' FROM `project.dataset.table` GROUP BY id ORDER BY id'
FROM (
  SELECT key 
  FROM `project.dataset.table`
  GROUP BY key
  ORDER BY key
)        

Note: the post you mentioned in your question was given in 2016 and for Legacy SQL

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement