【问题标题】:how can i pivot my sql table in bigquery?如何在 bigquery 中旋转我的 sql 表?
【发布时间】:2020-08-10 02:50:37
【问题描述】:

我正在尝试在 bigquery 中对表进行数据透视。 我有一个来自这个查询的表

SELECT category, salesRank, productDetail, productId FROM productTable;

但我想做这样的。

如何进行查询以显示这样的表格?

【问题讨论】:

标签: sql google-bigquery pivot


【解决方案1】:

你可以很容易地用你需要的东西构造一个数组:

select sales_rank,
       array_agg(struct(t.category, t.salesrank, t.productdetail, t.productid) order by min(t.productid) as products
from t
group by sales_rank;

您可以使用条件聚合进行透视:

select sales_rank,
       max(case when category = 'Bag' then salesrank end) as bag_salesrank,
       max(case when category = 'Bag' then productdetail end) as bag_productdetail,
       max(case when category = 'Bag' then productid end) as bag_productid,
       max(case when category = 'Wallet' then salesrank end) as wallet_salesrank,
       max(case when category = 'Wallet' then productdetail end) as wallet_productdetail,
       max(case when category = 'Wallet' then productid end) as wallet_productid,
       . . .
from t
group by sales_rank;

【讨论】:

  • 非常感谢!它并不像我想象的那样完美,但可以帮助我找到解决方案。
猜你喜欢
  • 2022-12-11
  • 2021-09-18
  • 1970-01-01
  • 2015-08-23
  • 2013-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多