【发布时间】:2019-01-24 19:25:24
【问题描述】:
我正在尝试使用标准 SQL 在 bigQuery 中旋转一个表 - 这是我的输入表 -
with temp as (
select "1" as id , "a" as source
union all
select "1" as id , "b" as source
union all
select "1" as id , "c" as source
union all
select "2" as id , "a" as source
union all
select "2" as id , "b" as source
union all
select "3" as id , "c" as source
union all
select "4" as id , "c" as source
)
select * from temp
我想根据source 列旋转此表,并根据派生源列的每个组合计算records 的数量。我只有 3 个来源 - a,b and c。
我的输出表应该是 -
source_a, source_b, source_c, records
0,0,0, 0
0,0,1, 2
0,1,0, 0
0,1,1, 0
1,0,0, 0
1,0,1, 0
1,1,0, 1
1,1,1, 1
我尝试过使用 case 语句,但我认为它不起作用 -
with temp as (
select "1" as id , "a" as source
union all
select "1" as id , "b" as source
union all
select "1" as id , "c" as source
union all
select "2" as id , "a" as source
union all
select "2" as id , "b" as source
union all
select "3" as id , "c" as source
union all
select "4" as id , "c" as source
)
select case when source = "a" then 1 else 0 end source_a,
case when source = "b" then 1 else 0 end source_b,
case when source = "c" then 1 else 0 end source_c,
count(*) as records
from temp
group by 1 ,2 ,3
【问题讨论】:
-
到目前为止你尝试了什么?
-
我尝试过 case 语句,但它没有捕获所有组合
标签: google-bigquery