大查询或SQL重塑数据

我使用bigquery存储数据

例如我有桌子

userId|event  |count
------------- |
1     |event1 |1
1     |event2 |2
2     |event1 |2
2     |event2 |1 
2     |event3 |4 
3     |event1 |3 
4     |event3 |5 
4     |event4 |5 

我怎么能得到这个表?(关于列事件{index}计数总和)
仅使用BigQuery(或SQL)的能力

userId|event1 |event2|event3|event4
----------------------------------
1     |1      |2     |0     |0    |
2     |2      |1     |4     |0    |
3     |0      |0     |0     |0    |
4     |0      |0     |5     |5    |

最佳答案 如果您只有少数事件可供您使用 – 您需要构建尽可能多的相应行,因为您有不同的事件.如果预期事件的数量不变 – 您可以随时轻松地构建此类查询,然后使用它

SELECT 
  userID,
  SUM(CASE WHEN event = 'event1' THEN [count] ELSE 0 END) AS event1,
  SUM(CASE WHEN event = 'event2' THEN [count] ELSE 0 END) AS event2,
  SUM(CASE WHEN event = 'event3' THEN [count] ELSE 0 END) AS event3,
  SUM(CASE WHEN event = 'event4' THEN [count] ELSE 0 END) AS event4
FROM YourTable
GROUP BY userId

如果你需要更动态的东西 – 看一下非常相似的例子https://stackoverflow.com/a/36623258/5221944

在您的情况下,构建动态SQL的查询将如下所示

SELECT 'SELECT userId, ' + 
   GROUP_CONCAT_UNQUOTED(
      'SUM(IF(event="'+event+'",[count],0)) as [d_'+REPLACE(event,'/','_')+']'
   ) 
   + ' FROM YourTable GROUP BY userId ORDER BY userId'
FROM (
  SELECT event FROM YourTable GROUP BY event ORDER BY event
)

注意以下行

      'SUM(IF(event="'+event+'",[count],0)) as [d_'+REPLACE(event,'/','_')+']'

它确保您的偶数名称符合字段/列名称的要求
如果您的evens总是看起来像event1,event2等,您可以简化此行并使用

      'SUM(IF(event = "' + event + '", [count], 0)) as ' + event
点赞