博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
小试下新博客,一个列传行的SQL
阅读量:7167 次
发布时间:2019-06-29

本文共 1459 字,大约阅读时间需要 4 分钟。

源表的结构如下:

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后面只能出现每组最小维的一行数据,只要保证这个我觉得就可以。

 

转载于:https://www.cnblogs.com/JSD1207ZX/p/9378795.html

你可能感兴趣的文章