源表的结构如下:
create table TAB_SAL( empno NUMBER, income_type NUMBER, income_amount NUMBER)
列转行SQL:
1 select t.empno, 2 (select sum(t1.income_amount) 3 from TAB_SAL t1 4 where t1.income_type = 11 5 and t1.empno = t.empno), 6 (select sum(t1.income_amount) 7 from TAB_SAL t1 8 where t1.income_type = 22 9 and t1.empno = t.empno),10 (select sum(t1.income_amount)11 from TAB_SAL t112 where t1.income_type = 3313 and t1.empno = t.empno),14 (select sum(t1.income_amount)15 from TAB_SAL t116 where t1.income_type = 4417 and t1.empno = t.empno)18 19 from TAB_SAL t20 group by t.empno
1 select t.empno, 2 (select t1.income_amount 3 from TAB_SAL t1 4 where t1.empno = t.empno 5 and t1.income_type = 11), 6 (select t1.income_amount 7 from TAB_SAL t1 8 where t1.empno = t.empno 9 and t1.income_type = 22),10 (select t1.income_amount11 from TAB_SAL t112 where t1.empno = t.empno13 and t1.income_type = 33),14 (select t1.income_amount15 from TAB_SAL t116 where t1.empno = t.empno17 and t1.income_type = 44)18 19 from TAB_SAL t20 21 group by t.empno
一个加sum一个不加,结果都可以,我认为分组完毕后select后面只能出现每组最小维的一行数据,只要保证这个我觉得就可以。