为什么这个sql RANK OVER()命令给我所有等级1

时间:2017-12-19 16:02:53

标签: sql-server-2008

在以下查询中,我尝试按最高计算WorstDerogLevel然后最低RecentDerogMonths对行进行排名,但它将所有行排名为1.

SELECT 
            strTransID,
            strRateCode as PreviousHighRateCode,
            strRateStatusCode as TradelineRateCode,
            dateReported,
            datePreviousHigh,
            CASE
                WHEN 
                    strRateStatusCode IN ('2', '3', '4', '5', '7', '8', '9', 'F', 'G', 'H', 'M', 'Z', '#', '$')
                    AND
                    (
                        strRateStatusCode IN ('F', 'G', 'H', 'M', 'Z', '#', '$')
                        OR strRateStatusCode >= strRateCode 
                    )
                THEN 
                    strRateStatusCode 
                ELSE 
                    strRateCode 
            END AS WorstDerogLevel
            ,
                DateDiff(month, 
                    CASE 
                        WHEN 
                            ( 
                                dateReported >= datePreviousHigh 
                                OR datePreviousHigh is null
                            )
                            AND strRateStatusCode IN ('2', '3', '4', '5', '7', '8', '9', 'F', 'G', 'H', 'M', 'Z', '#', '$')
                            AND
                            (
                                strRateStatusCode IN ('F', 'G', 'H', 'M', 'Z', '#', '$')
                                OR strRateStatusCode >= strRateCode 
                            )
                        THEN 
                            dateReported 
                        ELSE 
                            datePreviousHigh 
                    END, 
                    dateTransDate) as RecentDerogMonths

            ,

        RANK() OVER   
        (
            PARTITION BY 
                DateDiff(month, 
                    CASE 
                        WHEN 
                            ( 
                                dateReported >= datePreviousHigh 
                                OR datePreviousHigh is null
                            )
                            AND strRateStatusCode IN ('2', '3', '4', '5', '7', '8', '9', 'F', 'G', 'H', 'M', 'Z', '#', '$')
                            AND
                            (
                                strRateStatusCode IN ('F', 'G', 'H', 'M', 'Z', '#', '$')
                                OR strRateStatusCode >= CstrRateCode 
                            )
                        THEN 
                            dateReported 
                        ELSE 
                            datePreviousHigh 
                    END, 
                    dateTransDate) 

            ORDER BY

            CASE
                WHEN 
                    strRateStatusCode IN ('2', '3', '4', '5', '7', '8', '9', 'F', 'G', 'H', 'M', 'Z', '#', '$')
                    AND
                    (
                        strRateStatusCode IN ('F', 'G', 'H', 'M', 'Z', '#', '$')
                        OR strRateStatusCode >= strRateCode 
                    )
                THEN 
                    strRateStatusCode 
                ELSE 
                    strRateCode 
            END desc,

                DateDiff(month, 
                    CASE 
                        WHEN 
                            ( 
                                dateReported >= datePreviousHigh 
                                OR datePreviousHigh is null
                            )
                            AND strRateStatusCode IN ('2', '3', '4', '5', '7', '8', '9', 'F', 'G', 'H', 'M', 'Z', '#', '$')
                            AND
                            (
                                strRateStatusCode IN ('F', 'G', 'H', 'M', 'Z', '#', '$')
                                OR strRateStatusCode >= strRateCode 
                            )
                        THEN 
                            dateReported 
                        ELSE 
                            datePreviousHigh 
                    END, 
                    dateTransDate) asc

        ) AS Rank  

     FROM 
        [Customers]

    WHERE strTransID = '279579407'

这是我的结果集 enter image description here

1 个答案:

答案 0 :(得分:1)

只需使用PartitionField

上的表达式添加两列OrderFieldRANK() OVER来调试您的查询

有两种情况,每行都有RANK = 1

  1. 每行属于不同的分区,因此您有1个分区。
  2. 分区中的所有行都具有相同的值,均为1级。