原始数据
type line amount
1 a 11
1 b 22
2 a 111
2 b 222
1.列转行
select
type,
max(case when line='a' then amount else 0 end ) a,
max(case when line='b' then amount else 0 end ) b
from test
group by type
结果:
1 11 22
2 111 222
2.不同维度聚合
select
type,
case when type=1 then line else '-' end line,
sum(case when type=1 then 0 else amount end),
max(case when type=1 then amount else 0 end)
from test
group by
type,
case when type=1 then line else '-' end
结果:
1 a 0 11
1 b 0 22
2 - 333 0
使用max函数实现唯一性
最新推荐文章于 2021-09-28 00:08:13 发布