sql - 在 BigQuery 中将行转置为列(Pivot 实现)

标签 sql google-bigquery google-cloud-platform

我想生成一个新表,并使用 BigQuery 放置所有键值对,其中键作为列名称,值作为各自的值。

示例:

**Key**                  **Value**
channel_title           Mahendra Guru    
youtube_id              ugEGMG4-MdA  
channel_id              UCiDKcjKocimAO1tV    
examId                  72975611-4a5e-11e5   
postId                  1189e340-b08f 

channel_title           Ab Live  
youtube_id              3TNbtTwLY0U  
channel_id              UCODeKM_D6JLf8jJt    
examId                  72975611-4a5e-11e5   
postId                  0c3e6590-afeb

我想将其转换为:

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

如何使用 BigQuery 执行此操作?

最佳答案

BigQuery 尚不支持旋转函数
您仍然可以使用以下方法在 BigQuery 中执行此操作

但首先,除了输入数据中的两列之外,您还必须有一列来指定输入中需要合并为输出中的一行的行组

所以,我假设您的输入表(yourTable)如下所示

**id**  **Key**                  **Value**
   1    channel_title           Mahendra Guru    
   1    youtube_id              ugEGMG4-MdA  
   1    channel_id              UCiDKcjKocimAO1tV    
   1    examId                  72975611-4a5e-11e5   
   1    postId                  1189e340-b08f 

   2    channel_title           Ab Live  
   2    youtube_id              3TNbtTwLY0U  
   2    channel_id              UCODeKM_D6JLf8jJt    
   2    examId                  72975611-4a5e-11e5   
   2    postId                  0c3e6590-afeb  

所以,首先你应该运行下面的查询

SELECT 'SELECT id, ' + 
   GROUP_CONCAT_UNQUOTED(
      'MAX(IF(key = "' + key + '", value, NULL)) as [' + key + ']'
   ) 
   + ' FROM yourTable GROUP BY id ORDER BY id'
FROM (
  SELECT key 
  FROM yourTable
  GROUP BY key
  ORDER BY key
) 

上述查询的结果将是字符串(如果要格式化)如下所示

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 yourTable 
GROUP BY id 
ORDER BY id

您现在应该复制上面的结果(注意:您实际上不需要格式化它 - 我这样做只是为了演示)并将其作为正常查询运行

结果将如您所料

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

请注意:如果您可以自己构建适当的查询(如步骤 2 中所示)并且字段数量小且恒定或者是一次性交易,则可以跳过步骤 1。但第 1 步只是为您量身打造的辅助步骤,因此您可以随时快速创建!

If you are interested - you can see more about pivoting in my other posts.

How to scale Pivoting in BigQuery?
请注意 – 每个表有 10K 列的限制 – 因此您只能使用 10K 个组织。
您还可以看到下面的简化示例(如果上面的示例太复杂/冗长):
How to transpose rows to columns with large amount of the data in BigQuery/SQL?
How to create dummy variable columns for thousands of categories in Google BigQuery?
Pivot Repeated fields in BigQuery

关于sql - 在 BigQuery 中将行转置为列(Pivot 实现),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40761453/

相关文章:

使用 init() 进行 Google 云函数 Golang 单元测试

php - CodeIgniter Active Record 多个

python - Bigquery(和 pandas)- 确保数据插入一致性

javascript - 如何在规范化 SQL 中使用子数组保存和返回 JavaScript 对象

sql - 使用 Google bigquery 的日期中的两个日期之间的区别?

sql - 我们如何从BigQuery中删除重复数据并将其保存到另一个具有很多属性的表中

firebase - 云功能安全规则

google-cloud-platform - 在 Google Compute Engine 上打开端口 8080

sql - 带有格鲁吉亚符号的 Oracle 搜索问题

sql - Oracle SQL,寻找关于约束的想法