Oracle SQL PERCENT_RANK在多个分组中汇总数据

时间:2015-09-20 08:54:17

标签: sql oracle11g window-functions

我正在尝试根据不同的分组显示Percentile排名..我最初尝试的代码类似于我在下面的代码。

我没有意识到的问题是'over partition'不会忽略我的分组。由于我无法找到解决方法...我最终使用CTE将数据分组到我想要的百分位排名,然后对我需要的每个百分位组使用子查询(10+)。

是否有更有效的方法来完成我的目标?

SELECT 
,employee
,dept
,location
,shift
,fq
,fm
,day
,sum(calls)
,sum(handletime)

/*YTD P RANKS */ 

,PERCENT_RANK() over (partition by dept order by DECODE(sum(calls),0,0,sum(handletime) / sum(calls)) DESC) as YTD_P_RANK_ALL_LOCATIONS
,PERCENT_RANK() over (partition by dept, location by DECODE(sum(calls),0,0,sum(handletime) / sum(calls)) DESC) as YTD_P_RANK_LOCAL_LOCATION
,PERCENT_RANK() over (partition by dept, shift by DECODE(sum(calls),0,0,sum(handletime) / sum(calls)) DESC) as YTD_P_RANK_SHIFT

/*QTD P RANKS */ 

,PERCENT_RANK() over (partition by fq, dept order by DECODE(sum(calls),0,0,sum(handletime) / sum(calls)) DESC) as QTD_P_RANK_ALL_LOCATIONS
,PERCENT_RANK() over (partition by fq, dept, location by DECODE(sum(calls),0,0,sum(handletime) / sum(calls)) DESC) as QTD_P_RANK_LOCAL_LOCATION
,PERCENT_RANK() over (partition by fq, dept, shift by DECODE(sum(calls),0,0,sum(handletime) / sum(calls)) DESC) as QTD_P_RANK_SHIFT


from table1

group by 

,employee
,dept
,location
,shift
,fq
,fm
,day

1 个答案:

答案 0 :(得分:1)

(已修改)如果您通过3个步骤拆分查询,我认为您可以使用此功能:

  1. 首先计算总和。
  2. 计算百分比排名。
  3. 应用您的最终group by
  4. 希望这能让你更接近你想要做的事情。

    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