Oracle PIVOT 每月行数据到 12 个月列
Oracle PIVOT monthly row data to 12 monthly columns
我正在查询一个 table,它有这样的数据:
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
WHERE 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
求和,但我什至无法让它工作。
只使用条件聚合:
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;
我正在查询一个 table,它有这样的数据:
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
WHERE 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
求和,但我什至无法让它工作。
只使用条件聚合:
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;