我使用以下存储过程,到目前为止工作正常。
有人可以告诉我如何更改这个以获得每个嵌套选择的排名吗? 我正在寻找的是一个新的等级,它被添加到每个等级中,以便每个嵌套的选择以1,2,3等开始。
我的SP:
SELECT *
FROM (
SELECT
(
SELECT policy,
COUNT(*) AS count0
FROM Log_PE
WHERE CONVERT(DATE, dateEsc, 120) >= CONVERT(DATE, CONVERT(VARCHAR(6), DATEADD(month, -1, GETDATE()), 112) + '01', 112)
GROUP BY policy
ORDER BY count0 desc, policy
FOR XML PATH('currentMonth'), ELEMENTS, TYPE
)
UNION ALL
SELECT
(
SELECT policy,
COUNT(*) AS count1
FROM Log_PE
WHERE CONVERT(DATE, dateEsc, 120) >= CONVERT(DATE, CONVERT(VARCHAR(6), DATEADD(month, -1, GETDATE()), 112) + '01', 112)
GROUP BY policy
ORDER BY count1 desc, policy
FOR XML PATH('prevMonth2'), ELEMENTS, TYPE
)
UNION ALL
SELECT
(
SELECT policy,
COUNT(*) AS count2
FROM Log_PE
WHERE CONVERT(DATE, dateEsc, 120) >= CONVERT(DATE, CONVERT(VARCHAR(6), DATEADD(month, -2, GETDATE()), 112) + '01', 112)
GROUP BY policy
ORDER BY count2 desc, policy
FOR XML PATH('prevMonth3'), ELEMENTS, TYPE
)
UNION ALL
SELECT
(
SELECT policy,
COUNT(*) AS count3
FROM Log_PE
WHERE CONVERT(DATE, dateEsc, 120) >= CONVERT(DATE, CONVERT(VARCHAR(6), DATEADD(month, -3, GETDATE()), 112) + '01', 112)
GROUP BY policy
ORDER BY count3 desc, policy
FOR XML PATH('prevMonth4'), ELEMENTS, TYPE
)
UNION ALL
SELECT
(
SELECT policy,
COUNT(*) AS count4
FROM Log_PE
WHERE CONVERT(DATE, dateEsc, 120) >= CONVERT(DATE, CONVERT(VARCHAR(6), DATEADD(month, -4, GETDATE()), 112) + '01', 112)
GROUP BY policy
ORDER BY count4 desc, policy
FOR XML PATH('prevMonth5'), ELEMENTS, TYPE
)
UNION ALL
SELECT
(
SELECT policy,
COUNT(*) AS count5
FROM log_PE
WHERE CONVERT(DATE, dateEsc, 120) >= CONVERT(DATE, CONVERT(VARCHAR(6), DATEADD(month, -5, GETDATE()), 112) + '01', 112)
GROUP BY policy
ORDER BY count5 desc, policy
FOR XML PATH('prevMonth5'), ELEMENTS, TYPE
)
) AS Data(XmlData)
FOR XML PATH(''), ELEMENTS, TYPE
END
非常感谢你提供任何帮助,蒂姆。
答案 0 :(得分:1)
将此列添加到每个嵌套选择中:
rank() over(order by count(*) desc, policy) [Rank]