BigQuery: 在BigQuery中,根据行值将行(在分组操作后)转换为列。

huangapple go评论53阅读模式
英文:

Bigquery: To convert rows(on groupby) to columns in bigquery w.r.t. row value

问题

需要将上面的表格转换/转置为BigQuery中的下面格式。

英文:

Have got a table data like below (sample data provided for single store, this data repeats for multiple store)
BigQuery: 在BigQuery中,根据行值将行(在分组操作后)转换为列。

Need to convert/transpose above table to below format in bigquery
BigQuery: 在BigQuery中,根据行值将行(在分组操作后)转换为列。

答案1

得分: 1

根据您的需求,您可以考虑使用PIVOT运算符。

例如:

select * from (
  select * except(category)
  from `projectId.dataset.table`
)
pivot (sum(sales) as sales, sum(item_sold) as item_sold for (type) in ('Loose', 'PP'))

输出:

BigQuery: 在BigQuery中,根据行值将行(在分组操作后)转换为列。

但是,根据您的要求,使用PIVOT运算符不可能返回一个名为sales PP fruit的列。当使用单个PIVOT列时,我们只能包括一个for条件。

要进行多列的数据透视,您需要使用多个Pivot。您可以参考这个堆栈链接

英文:

For your requirement you can consider using PIVOT operator.

For example:

select * from (
  select * except(category)
  from `projectId.dataset.table`
)
pivot (sum(sales) as sales,sum(item_sold) as item_sold for (type) in ('Loose','PP')) 

Output:

BigQuery: 在BigQuery中,根据行值将行(在分组操作后)转换为列。

But using pivot, as per your requirement it's not possible to return a column as sales PP fruit. When using a single PIVOT column we can only include one for condition.

To pivot multiple column you need to use Muliple Pivot's.You can refer to this stack link.

huangapple
  • 本文由 发表于 2023年2月27日 14:25:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577325.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定