我在行中有月度数据,但我需要通过“月度列”而不是一个合并的“月度”列来转换数据,您能否在这里提供帮助,以查看是否可以从“目标”表创建视图以按“月度”列进行分组?
来源
Account DAY CENTER ENTITY YEAR FREQUENCY AMOUNT MONTH
A010100001000 D02 CC124 BBC0181 FY15 PATD -3185791.08 Jan
A010100001000 D02 CC13L BBC0181 FY15 PATD -156955.28 Feb
A010100001000 D02 CC11B BBC0181 FY15 PATD -666443.03 Mar
A010100001000 D02 CCR81 BBC0181 FY15 PATD -178392.23 Apr
A010100001000 D02 CCR82 BBC0181 FY15 PATD -192553.44 May
A010100001000 D02 CC10M BBC0181 FY15 PATD -457125.95 Jun
A010100001000 D02 CC13P BBC0181 FY15 PATD -41853.95 Jul
A010100001000 D02 CCR53 BBC0181 FY15 PATD -110299.8 Aug
A010100001000 D02 CCR54 BBC0181 FY15 PATD -182933.93 Sep
A010100001000 D02 CC11C BBC0181 FY15 PATD -468148.55 Oct
A010100001000 D02 CCR71 BBC0181 FY15 PATD -133119.11 Nov
A010100001000 D02 CCW4A BBC0181 FY15 PATD -201339.86 Dec
所需的输出
Account DAY CENTER ENTITY YEAR FREQUENCY Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
A010100001000 D02 CC10M BBC0181 FY15 PATD -457125.95
A010100001000 D02 CC11B BBC0181 FY15 PATD -666443.03
A010100001000 D02 CC11C BBC0181 FY15 PATD -468148.55
A010100001000 D02 CC124 BBC0181 FY15 PATD -3185791.08
A010100001000 D02 CC13L BBC0181 FY15 PATD -156955.28
A010100001000 D02 CC13P BBC0181 FY15 PATD -41853.95
A010100001000 D02 CCR53 BBC0181 FY15 PATD -110299.8
A010100001000 D02 CCR54 BBC0181 FY15 PATD -182933.93
A010100001000 D02 CCR71 BBC0181 FY15 PATD -133119.11
A010100001000 D02 CCR81 BBC0181 FY15 PATD -178392.23
A010100001000 D02 CCR82 BBC0181 FY15 PATD -192553.44
A010100001000 D02 CCW4A BBC0181 FY15 PATD -201339.86
答案 0 :(得分:1)
我本来建议您使用PIVOT,但我发现您希望它们是单独的行,而不是全部都排成一行。您可以执行以下操作:
SELECT ACCOUNT, DAY, CENTER, ENTITY, YEAR, FREQUENCY,
CASE WHEN MONTH = 'Jan'
THEN AMOUNT
END "Jan"
CASE WHEN MONTH = 'Feb'
THEN AMOUNT
END "Feb"
.... FROM TABLENAME
答案 1 :(得分:0)
您想要的基本模式是每个月都有一个case表达式,并且仅在月份匹配时才显示金额:
select account, day, center, entity, year, frequency,
case when month = 'Jan' then amount end as jan,
case when month = 'Feb' then amount end as feb,
case when month = 'Mar' then amount end as mar,
-- ... all the other months
case when month = 'Dec' then amount end as dec
from source;
ACCOUNT DAY CENTE ENTITY YEAR FREQ JAN FEB MAR ... DEC
------------- --- ----- ------- ---- ---- ---------- ---------- ---------- ... ----------
A010100001000 D02 CC124 BBC0181 FY15 PATD -3185791.1
A010100001000 D02 CC13L BBC0181 FY15 PATD -156955.28
A010100001000 D02 CC11B BBC0181 FY15 PATD -666443.03
...
A010100001000 D02 CCW4A BBC0181 FY15 PATD ... -201339.86
如果您的输出中连续有多个条目,则它更接近于常规枢轴,但是您仍然可以通过添加聚合和匹配的分组来做同样的事情:
select account, day, center, entity, year, frequency,
max(case when month = 'Jan' then amount end) as jan,
max(case when month = 'Feb' then amount end) as feb,
max(case when month = 'Mar' then amount end) as mar,
-- ...
max(case when month = 'Dec' then amount end) as dec
from source
group by account, day, center, entity, year, frequency;
答案 2 :(得分:0)
我认为这应该有效。
WITH tmp AS
(
SELECT 'A010100001000' Account, 'D02' as day, 'CC124' center, 'BBC0181' entity, 'FY15' year, 'PATD' FREQUENCY , -3185791.08 amount, 'Jan' mnth FROM dual UNION
SELECT 'A010100001000', 'D02', 'CC13L', 'BBC0181', 'FY15', 'PATD', -156955.28, 'Feb' FROM dual UNION
SELECT 'A010100001000', 'D02', 'CC11B', 'BBC0181', 'FY15', 'PATD', -666443.03, 'Mar' FROM dual UNION
SELECT 'A010100001000', 'D02', 'CCR81', 'BBC0181', 'FY15', 'PATD', -178392.23, 'Apr' FROM dual UNION
SELECT 'A010100001000', 'D02', 'CCR82', 'BBC0181', 'FY15', 'PATD', -192553.44, 'May' FROM dual UNION
SELECT 'A010100001000', 'D02', 'CC10M', 'BBC0181', 'FY15', 'PATD', -457125.95, 'Jun' FROM dual UNION
SELECT 'A010100001000', 'D02', 'CC13P', 'BBC0181', 'FY15', 'PATD', -41853.95, 'Jul' FROM dual UNION
SELECT 'A010100001000', 'D02', 'CCR53', 'BBC0181', 'FY15', 'PATD', -110299.8 , 'Aug' FROM dual UNION
SELECT 'A010100001000', 'D02', 'CCR54', 'BBC0181', 'FY15', 'PATD', -182933.93, 'Sep' FROM dual UNION
SELECT 'A010100001000', 'D02', 'CC11C', 'BBC0181', 'FY15', 'PATD', -468148.55, 'Oct' FROM dual UNION
SELECT 'A010100001000', 'D02', 'CCR71', 'BBC0181', 'FY15', 'PATD', -133119.11, 'Nov' FROM dual UNION
SELECT 'A010100001000', 'D02', 'CCW4A', 'BBC0181', 'FY15', 'PATD', -201339.86, 'Dec' FROM dual
)
SELECT
*
FROM tmp
PIVOT
(
MAX(amount) FOR mnth IN ('Jan' Jan, 'Feb' feb, 'Mar' Mar, 'Apr' Apr, 'May' May, 'Jun' Jun, 'Jul' Jul, 'Aug' Aug, 'Sep' Sep, 'Oct' Oct, 'Nov' Nov, 'Dec' Dec)
);