感谢任何人对此的看法。 最近,我必须“降级”到Oracle数据库,在那里我有一个在11G中运行的Oracle SQL PIVOT视图,我现在必须重新创建这个支持旧学校的“Group by”方法。这是我的PIVOT视图。有没有PIVOT算子我怎么能重写这个视图的想法?
select JOB_TITLE,
COUNTRY,
fte_exp_yrs,
fte_alloc,
first_date
from ROKPI_HC_V3
pivot (sum(fte_alloc) as mo for (FIRST_DATE) IN
('01-JAN-15' AS JAN_15,
'01-FEB-15' AS FEB_15,
'01-MAR-15' AS MAR_15,
'01-APR-15' AS APR_15,
'01-MAY-15' AS MAY_15,
'01-JUN-15' AS JUN_15,
'01-JUL-15' AS JUL_15,
'01-AUG-15' AS AUG_15,
'01-SEP-15' AS SEP_15,
'01-OCT-15' AS OCT_15,
'01-NOV-15' AS NOV_15,
'01-DEC-15' AS DEC_15
))
where JOB_TITLE = 'MANAGER' or JOB_TITLE = 'NONMANAGER' OR JOB_TITLE = 'CPA'
order by country;
答案 0 :(得分:1)
'old school'方法是将您在pivot中使用的相同聚合函数与first_date
的每个值的case语句组合,并将其他列组合在一起:
select JOB_TITLE,
COUNTRY,
fte_exp_yrs,
sum(case when FIRST_DATE = '01-JAN-15' then fte_alloc end) AS JAN_15,
sum(case when FIRST_DATE = '01-FEB-15' then fte_alloc end) AS FEB_15,
sum(case when FIRST_DATE = '01-MAR-15' then fte_alloc end) AS MAR_15,
sum(case when FIRST_DATE = '01-APR-15' then fte_alloc end) AS APR_15,
sum(case when FIRST_DATE = '01-MAY-15' then fte_alloc end) AS MAY_15,
sum(case when FIRST_DATE = '01-JUN-15' then fte_alloc end) AS JUN_15,
sum(case when FIRST_DATE = '01-JUL-15' then fte_alloc end) AS JUL_15,
sum(case when FIRST_DATE = '01-AUG-15' then fte_alloc end) AS AUG_15,
sum(case when FIRST_DATE = '01-SEP-15' then fte_alloc end) AS SEP_15,
sum(case when FIRST_DATE = '01-OCT-15' then fte_alloc end) AS OCT_15,
sum(case when FIRST_DATE = '01-NOV-15' then fte_alloc end) AS NOV_15,
sum(case when FIRST_DATE = '01-DEC-15' then fte_alloc end) AS DEC_15
from ROKPI_HC_V3
where JOB_TITLE = 'MANAGER' or JOB_TITLE = 'NONMANAGER' OR JOB_TITLE = 'CPA'
group by JOB_TITLE, COUNTRY, fte_exp_yrs
order by country;
这正是Oracle所做的事情under the hood with a pivot。
如果FIRST_DATE
是日期列而不是字符串 - 我希望它是 - 那么你应该使用实际日期而不是依赖隐式转换;如果您确定会话日期语言将始终为英语,或者在该转换中指定NLS_DATE_LANGUAGE(如Kordiko在评论中显示),或者使用不是NLS敏感的其他格式掩码,则使用to_date('01-JAN-15', 'DD-MON-RR')
;或者使用ANSI日期文字:
sum(case when FIRST_DATE = date '2015-01-01' then fte_alloc end) AS JAN_15,