场景描述:表中分地市以多个维度存入数据,两个字段中各有两个标志位表示不同含义,现在需要以地市为单位展示不同维度的四个数据,即多行转多列
分析:之前使用pviot或者case when,但是只限于单条件,现使用多条件查询
select tt.name,
sum(case
when tt.DIRECTION = 1 and tt.type = 1 then
tt.VALUE else 0 end) avgin,
sum(case
when tt.DIRECTION = 2 and tt.type = 1 then
tt.VALUE else 0 end) avgout,
sum(case
when tt.DIRECTION = 1 and tt.type = 4 then
tt.VALUE else 0 end) maxin,
sum(case
when tt.DIRECTION = 2 and tt.type = 4 then
tt.VALUE else 0 end) maxout
from tab_test tt
group by tt.name