max和rank函数的结果不正确

时间:2018-03-07 15:58:19

标签: oracle

我正在尝试使用最新的psi分数返回一行,但我的代码返回多行,而实际需要的行将作为结果集的最后一行返回。我该怎么做才能解决这个问题。

S

ELECT REQUEST_NUM,PI_CANDIDATE_NUM,
    COALESCE (MAX_GCO_AD_KNOWLEDGE_TEST_SCORE, MAX_PSI_OVERALL_SCORE) AS PSI_SCORE
    --COALESCE(MAX_GCO_AD_KNOWLEDGE_TEST_SCORE,MAX_PSI_OVERALL_SCORE)-39.035024/8.439997 AS PSI XMation


    FROM(

    SELECT
    REQUEST_NUM,PI_CANDIDATE_NUM,
    MAX(AA.PSI_OVERALL_SCORE) KEEP (DENSE_RANK FIRST ORDER BY AA.ARANK) MAX_PSI_OVERALL_SCORE,
    MAX(AA.GCO_AD_KNOWLEDGE_TEST_SCORE) KEEP (DENSE_RANK FIRST ORDER BY AA.ARANK) MAX_GCO_AD_KNOWLEDGE_TEST_SCORE

    FROM (
    select  
      RANK() OVER (PARTITION BY REQUEST_NUM ORDER BY REQUEST_LAST_MODIFIED_DT ) ARANK
      ,PARENT_PI_NUMBER,REQUEST_NUM,PI_CANDIDATE_NUM,PSI_OVERALL_SCORE,GCO_AD_KNOWLEDGE_TEST_SCORE,REQUEST_LAST_MODIFIED_DT
    from  
       WC_APPLICATION_EVENT_F

    -- GCO_AD_KNOWLEDGE_TEST_SCORE != '10100' and 
      where PI_CANDIDATE_NUM = '4173093'

    --nd GCO_AD_KNOWLEDGE_TEST_SCORE is  null
    ) AA
    --where AA.ARANK=1


    GROUP BY REQUEST_NUM,PI_CANDIDATE_NUM
    --ORDER By PARENT_PI_NUMBER,PI_NUMBER,REQUEST_LAST_MODIFIED_DT;


    )
    BB;

Sample data:

    REQUEST_NUM PI_CANDIDATE_NUM REQUEST_LAST_MODIFIED_DT PSI_SCORE
    ----------- ---------------- ------------------------ ---------
                         4639022        
        1655626          4639022 5-Mar-17
        1662401          4639022 8-Mar-17                 22.6
        1662470          4639022 6-Apr-17   
        1662486          4639022 6-Apr-17   
        1662499          4639022 8-Mar-17                 30.3
        1771817          4639022 7-Jun-17                 35.3
        1797323          4639022 24-Jun-17                38.5

My expected results is the last row with a value of 38.5 since has the latest date.

1 个答案:

答案 0 :(得分:0)

这是一种方法。它假设您输入了一个特定的候选项(否则可以修改子查询中的分析函数以获得每个候选项的最新NON-NULL分数。)

编辑:如果您需要检索所有候选人的最新分数,则:(1)删除在内部查询中过滤单个候选人的条件; (2)在partition by pi_candidate_num之后(在左括号之后)添加rank() over (并在order by ...之前添加一个空格(仍然在over(.....)的{​​{1}}子句中)。的 \修改

如果在同一天完成两个NON-NULL分数(可能这在您的数据中是不可能的,但如果它是......)那么将返回两行。

我认为候选人号码是NUMBER,所以我删除了条件中的单引号;我更改了候选人编号以匹配您的样本输入。

WITH子句不是查询的一部分 - 在尝试解决方案之前将其删除。

rank()