用SUM()函数实现交叉数据报表
1 用SUM()函数实现交叉数据报表:
有下面的数据:
Col1 Col2 col3
AAAA aaaa 2
AAAA bbbb 5
AAAA cccc 6
BBBB aaaa 3
BBBB dddd 2
CCCC bbbb 3
CCCC dddd 2
如何用SQL 转换成下面的形式:
AAAA BBBB CCCC
aaaa 2 3 0
bbbb 5 3
cccc 6
dddd 2 2
实现:
select col2, SUM(decode(col1,'AAAA',COL3,NULL)) AAAA,
SUM(decode(col1,'BBBB',COL3,NULL)) BBBB,
SUM(decode(col1,'CCCC',COL3,NULL)) CCCC
FROM TEST1
GROUP BY COL2;