早期 - 枢轴 table
Presto - pivot table
嗨,我有一个 table 这样的:
Session id Property
1 Radar
2 Bullet
1 Bullet
3 Radar
2 Price
我想转换成这样:
Radar Bullet Price
1 1
2 2
3
对于固定的属性列表,您可以进行条件聚合:
select session_id,
max(case when property = 'Radar' then 1 else 0 end) as radar,
max(case when property = 'Bullet' then 1 else 0 end) as bullet,
max(case when property = 'Price' then 1 else 0 end) as price
from mytable
group by session_id
这会将会话 ID 放在第一列中,并将 0
/1
值放在每列中,具体取决于给定会话是否拥有给定的 属性.
要生成您显示的准确输出(这可能不如上面的有用),您可以这样做:
select
case when max(case when property = 'Radar' then 1 else 0 end) = 1 then session_id end as radar,
case when max(case when property = 'Bullet' then 1 else 0 end) = 1 then session_id end as bullet,
case when max(case when property = 'Price' then 1 else 0 end) = 1 then session_id end as price
from mytable
group by session_id
嗨,我有一个 table 这样的:
Session id Property
1 Radar
2 Bullet
1 Bullet
3 Radar
2 Price
我想转换成这样:
Radar Bullet Price
1 1
2 2
3
对于固定的属性列表,您可以进行条件聚合:
select session_id,
max(case when property = 'Radar' then 1 else 0 end) as radar,
max(case when property = 'Bullet' then 1 else 0 end) as bullet,
max(case when property = 'Price' then 1 else 0 end) as price
from mytable
group by session_id
这会将会话 ID 放在第一列中,并将 0
/1
值放在每列中,具体取决于给定会话是否拥有给定的 属性.
要生成您显示的准确输出(这可能不如上面的有用),您可以这样做:
select
case when max(case when property = 'Radar' then 1 else 0 end) = 1 then session_id end as radar,
case when max(case when property = 'Bullet' then 1 else 0 end) = 1 then session_id end as bullet,
case when max(case when property = 'Price' then 1 else 0 end) = 1 then session_id end as price
from mytable
group by session_id