(编辑)我想你可能会得到这个工作,如果你在3个步骤分割你的查询:
- 首先计算的款项。
- 计算百分比排名。
- 应用您的最终
group by
。
希望这能让你更接近你想要做的事。
WITH SumView AS (
SELECT t.*
,sum(calls) over (partition by dept) AS sum_call_by_dept
,sum(handletime) over (partition by dept) AS sum_ht_by_dept
,sum(calls) over (partition by dept, location) AS sum_call_by_dept_loc
,sum(handletime) over (partition by dept, location) AS sum_ht_by_dept_loc
,sum(calls) over (partition by dept, shift) AS sum_call_by_dept_shift
,sum(handletime) over (partition by dept, shift) AS sum_ht_by_dept_shift
,sum(calls) over (partition by fq, dept) AS sum_call_by_fq_dept
,sum(handletime) over (partition by fq, dept) AS sum_ht_by_fq_dept
,sum(calls) over (partition by fq, dept, location) AS sum_call_by_fq_dept_loc
,sum(handletime) over (partition by fq, dept, location) AS sum_ht_by_fq_dept_loc
,sum(calls) over (partition by fq, dept, shift) AS sum_call_by_fq_dept_shift
,sum(handletime) over (partition by fq, dept, shift) AS sum_ht_by_fq_dept_shift
FROM table1 t
), PercentRankView AS (
SELECT t.*
/*YTD P RANKS */
,PERCENT_RANK() over (partition by dept order by DECODE(sum_call_by_dept,0,0,sum_ht_by_dept/sum_call_by_dept) DESC) as YTD_P_RANK_ALL_LOCATIONS
,PERCENT_RANK() over (partition by dept, location by DECODE(sum_call_by_dept_loc,0,0,sum_ht_by_dept_loc/sum_call_by_dept_loc) DESC) as YTD_P_RANK_LOCAL_LOCATION
,PERCENT_RANK() over (partition by dept, shift by DECODE(sum_call_by_dept_shift,0,0,sum_ht_by_dept_shift/sum_call_by_dept_shift) DESC) as YTD_P_RANK_SHIFT
/*QTD P RANKS */
,PERCENT_RANK() over (partition by fq, dept order by DECODE(sum_call_by_fq_dept,0,0,sum_ht_by_fq_dept/sum_call_by_fq_dept) DESC) as QTD_P_RANK_ALL_LOCATIONS
,PERCENT_RANK() over (partition by fq, dept, location by DECODE(sum_call_by_fq_dept_loc,0,0,sum_ht_by_fq_dept_loc/sum_call_by_fq_dept_loc) DESC) as QTD_P_RANK_LOCAL_LOCATION
,PERCENT_RANK() over (partition by fq, dept, shift by DECODE(sum_call_by_fq_dept_shift,0,0,sum_ht_by_fq_dept_shift/sum_call_by_fq_dept_shift) DESC) as QTD_P_RANK_SHIFT
FROM SumView t
)
SELECT employee
,dept
,location
,shift
,fq
,fm
,day
,sum(calls)
,sum(handletime)
,YTD_P_RANK_ALL_LOCATIONS
,YTD_P_RANK_LOCAL_LOCATION
,YTD_P_RANK_SHIFT
,QTD_P_RANK_ALL_LOCATIONS
,QTD_P_RANK_LOCAL_LOCATION
,QTD_P_RANK_SHIFT
FROM PercentRankView
GROUP BY employee
,dept
,location
,shift
,fq
,fm
,day
,YTD_P_RANK_ALL_LOCATIONS
,YTD_P_RANK_LOCAL_LOCATION
,YTD_P_RANK_SHIFT
,QTD_P_RANK_ALL_LOCATIONS
,QTD_P_RANK_LOCAL_LOCATION
,QTD_P_RANK_SHIFT
你是什么意思*“我没有意识到的问题是''分区'不会忽略我的分组”* - 你的意思是你没有意识到窗口函数(和他们的'PARTITION BY'子句)是根据“GROUP BY”子句的结果进行评估的?另外,与您有不同的结果格式,例如不同的百分比等级放在不同的行中,而不是全部放在一行中? –
是的..我从来没有真正使用PARTITION BY之前,我最初认为它是独立的选择分组。 我对Oracle SQL/PL有点新鲜。我会诚实地说,我通常只是对事情进行修改,直到他们做我需要他们做的事情......这一个让我陷入困境。 将结果放在自己的行中的想法对我来说似乎是更好的方法。我很乐意提供任何帮助。我刚刚检查了..现在要做163行。 (15个CTEs,12个subquerys) – yeahthisisrob