Oracle PIVOT每月行数据为12个月的列

时间:2015-03-12 21:46:05

标签: sql oracle pivot transpose

我正在查询有这样数据的表:

ACT  CY  GRP   MONTH     AMT
111 CY15 AAA 2015-01-01  2.3
222 CY15 AAA 2015-01-01  3.1 
111 CY15 AAA 2015-02-01 11.5  
222 CY15 AAA 2015-02-01 44.6
111 CY15 AAA 2015-03-01  4.9
222 CY15 AAA 2015-03-01  3.8
222 CY15 AAA 2015-04-01    0  
111 CY15 AAA 2015-04-01 49.8
111 CY15 AAA 2015-05-01    0 
222 CY15 AAA 2015-05-01 83.0
111 CY15 AAA 2015-06-01    0 
222 CY15 AAA 2015-06-01 79.7
111 CY15 AAA 2015-07-01 66.4
222 CY15 AAA 2015-07-01    0 
222 CY15 AAA 2015-08-01 25.5
111 CY15 AAA 2015-08-01 63.1
222 CY15 AAA 2015-09-01 30.4
111 CY15 AAA 2015-09-01 79.7
111 CY15 AAA 2015-10-01 28.0
222 CY15 AAA 2015-10-01 66.4
222 CY15 AAA 2015-11-01 66.4
111 CY15 AAA 2015-11-01 28.3
111 CY15 AAA 2015-12-01 79.7
222 CY15 AAA 2015-12-01 30.5
333 CY16 AAA 2016-01-01 25.4
444 CY16 AAA 2016-01-01 63.1
444 CY16 AAA 2016-02-01 56.4
333 CY16 AAA 2016-02-01 20.2
444 CY16 AAA 2016-03-01 22.4
333 CY16 AAA 2016-03-01 59.8
333 CY16 AAA 2016-04-01 58.7
444 CY16 AAA 2016-04-01 19.5
333 CY16 AAA 2016-05-01 19.6
444 CY16 AAA 2016-05-01 58.7
444 CY16 AAA 2016-06-01 11.7
333 CY16 AAA 2016-06-01 70.4
333 CY16 AAA 2016-07-01 58.7
444 CY16 AAA 2016-07-01 19.5
444 CY16 AAA 2016-08-01 58.7
333 CY16 AAA 2016-08-01 19.3
333 CY16 AAA 2016-09-01 11.3
444 CY16 AAA 2016-09-01 70.4
333 CY16 AAA 2016-10-01 55.7
333 CY16 AAA 2016-11-01 14.3
333 CY16 AAA 2016-12-01 19.3
555 CY15 BBB 2015-01-01 58.7
555 CY15 BBB 2015-02-01 70.4
555 CY15 BBB 2015-03-01 13.6
555 CY15 BBB 2015-04-01 55.7
555 CY15 BBB 2015-05-01 49.8
555 CY15 BBB 2015-06-01 58.7
555 CY15 BBB 2015-07-01 65.9
555 CY15 BBB 2015-08-01 19.0
555 CY15 BBB 2015-09-01 15.9
555 CY15 BBB 2015-10-01 87.9
555 CY15 BBB 2015-11-01 83.5
555 CY15 BBB 2015-12-01 10.7
666 CY16 BBB 2016-01-01 87.9
666 CY16 BBB 2016-02-01 87.9
666 CY16 BBB 2016-03-01 15.4
666 CY16 BBB 2016-04-01 83.5
666 CY16 BBB 2016-05-01 74.7
666 CY16 BBB 2016-06-01 79.1
666 CY16 BBB 2016-07-01 47.9
666 CY16 BBB 2016-08-01 47.9
666 CY16 BBB 2016-09-01 57.5
666 CY16 BBB 2016-10-01 47.9
666 CY16 BBB 2016-11-01 47.9
666 CY16 BBB 2016-12-01 57.5

我正试图将其显示如下

GRP CY January February March ... December
AAA 15   5.4     56.1    AMT  ...    AMT
AAA 16   AMT     AMT     AMT  ...    AMT
BBB 15   AMT     AMT     AMT  ...    AMT
BBB 16   AMT     AMT     AMT  ...    AMT

在哪里AMT是该组和同一日历年中该月份的所有帐户金额的总和。

我的第一次尝试

select * from (select p.grp, p.cy, p.month, p.amt from phasing p WHERE
p.act IN  ('111', '222', '333', '444', '555', '666') ) pivot( sum(amt) 
FOR to_char(to_date(month, 'YYYY-DD-MM'), 'Month')
in 'January', 'February', 'March', 'April')    )

ORA-01738:缺少IN关键字

这至少没有错误,但我并不总是知道Month日期

select * from (select p.grp, p.cy, p.month, p.amt from phasing p WHERE
p.act IN  ('111', '222', '333', '444', '555', '666') ) pivot( sum(amt) 
FOR month in (to_date('2015-01-01', 'YYYY-MM-DD') As Jan, to_date('2015-02-01', 'YYYY-MM-DD') As Feb)    )

我想我在第一次尝试时需要一个GROUP BY,这样我就不会在每个日历年总结所有Januarys,但我甚至无法让它工作。

1 个答案:

答案 0 :(得分:2)

只需使用条件聚合:

select act, cy,
       sum(case when to_char(month, 'MM') = '01' then amt end) as Jan,
       sum(case when to_char(month, 'MM') = '02' then amt end) as Feb,
       . . .,
       sum(case when to_char(month, 'MM') = '12' then amt end) as Dec
from phasing p
group by act, cy;