[一]、行转列
select t.TOWN_ID as TOWN_ID, sum(decode(t.STATE, '筹建', t.COUNT1,0)) as STATE1, sum(decode(t.STATE, '临时', t.COUNT1,0)) as STATE2, sum(decode(t.STATE, '正常', t.COUNT1,0)) as STATE3 from (select TOWN_ID,STATE,count(*) COUNT1 from TAX_INFO group by TOWN_ID,STATE order by TOWN_ID,STATE) t group by t.TOWN_ID
结果如下
[二]、列转行
select TOWN_ID, '筹建' STATE, STATE1 as COUNT from HY_STATE union select TOWN_ID, '临时' STATE, STATE2 as COUNT from HY_STATE union select TOWN_ID, '正常' STATE, STATE3 as COUNT from HY_STATE
结果如下