在以下查询中,我尝试按最高计算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'
答案 0 :(得分:1)
只需使用PartitionField
OrderField
和RANK() OVER
来调试您的查询
有两种情况,每行都有RANK = 1