0

因此,我真的很高兴能够根据生效日期对结果进行排名,但目前我遇到了一个问题,即一个数据元素重复 (POD) 而另一个数据元素根据 EFFDT (DEPT) 发生变化。

我只想对 Pod 的唯一值进行排名,然后是 Dept。但是 Pod 是基于 Dept 的,Dept 变化更频繁。下面的代码给了我:

EENBR  PodRank POD DeptRank DeptNbr   DeptEffdt
100     1       73    1    12420      4/11/2005
100     2       73    2    12560      5/22/2005
100     3       73    3    12501      6/24/2007
200     1       12    1    50768      3/14/2005
200     2       13    2    10949      9/9/2012
300     1       73    1    12450      3/21/2005
300     2       73    2    12471      12/25/2005
300     3       73    3    12581      12/21/2008
300     4       73    4    12585      6/6/2010
300     5       73    5    12432      5/19/2013



SELECT DISTINCT 
       AL4.FULL_NAME,
       AL4.EMPLOYEE_NUMBER,
       dense_rank() over (partition by AL4.EMPLOYEE_NUMBER
                          order by AL3.EFFECTIVE_START_DATE) as POD_RANKING,
       AL7.POD_NBR as POD,
       row_number() over (partition by AL4.EMPLOYEE_NUMBER
                          order by AL3.EFFECTIVE_START_DATE) as DEPT_RANKING,
       AL3.RECORDVALUE AS DEPT_NUMBER,
       AL3.EFFECTIVE_START_DATE AS "DEPT EFFECTIVE DATE"     
  FROM T1 AL3,
       T2 AL4,
       T3 AL7 
 WHERE AL4.PERSON_ID = AL3.PERSON_ID
   AND AL4.EMPLOYEE_NUMBER = AL3.EMPLOYEE_NUMBER
   AND AL3.RECORDTYPE = 'DEPARTMENT_NUMBER'
   AND AL7.DEPT_NBR = AL3.RECORDVALUE
 Order By AL4.Employee_Number;

是否有仅对唯一值进行排名的函数?

4

2 回答 2

1

您正在寻找的功能是分析功能dense_rank()

dense_rank() over (partition by eenbr order by pod) as ranking

这是得到你想要的最简单的方法。您可以将其添加到select查询的子句中。

于 2013-09-18T18:36:43.827 回答
0

没有这个功能,但是当你使用嵌套的窗口函数时你可以得到结果:

SELECT dt.*,
   SUM(flag) OVER (PARTITION BY EMPLOYEE_NUMBER
                   ORDER BY "DEPT EFFECTIVE DATE") AS POD_RANKING
FROM
 ( 
   SELECT
          AL4.FULL_NAME,
          AL4.EMPLOYEE_NUMBER,
          AL7.POD_NBR AS POD,
          ROW_NUMBER() OVER (PARTITION BY AL4.EMPLOYEE_NUMBER
                             ORDER BY AL3.EFFECTIVE_START_DATE) AS DEPT_RANKING,
          AL3.RECORDVALUE AS DEPT_NUMBER,
          AL3.EFFECTIVE_START_DATE AS "DEPT EFFECTIVE DATE",  
          CASE WHEN ROW_NUMBER() 
                    OVER (PARTITION BY AL4.EMPLOYEE_NUMBER,AL7.POD_NBR
                          ORDER BY AL3.EFFECTIVE_START_DATE) = 1 THEN 1 ELSE 0 END AS flag
     FROM T1 AL3,
          T2 AL4,
          T3 AL7 
    WHERE AL4.PERSON_ID = AL3.PERSON_ID
      AND AL4.EMPLOYEE_NUMBER = AL3.EMPLOYEE_NUMBER
      AND AL3.RECORDTYPE = 'DEPARTMENT_NUMBER'
      AND AL7.DEPT_NBR = AL3.RECORDVALUE
  ) dt
ORDER BY AL4.Employee_Number;

编辑:好的,我注意到这是一个具有不同顺序的简单 DENSE_RANK 的过于复杂的版本,就在戈登发布他的答案之前不久:-)

dense_rank() over (partition by AL4.EMPLOYEE_NUMBER order by AL7.POD_NBR)
于 2013-09-18T18:14:35.463 回答