在SQL Server 2008中,我RANK() OVER (PARTITION BY Col2 ORDER BY Col3 DESC)
用来返回带有的数据集RANK
。但是我每个分区都有数百条记录,因此我将从1、2、3 ...... 999列中获取值。但我只希望RANKs
每个最多2 个PARTITION
。
例:
ID Name Score Subject
1 Joe 100 Math
2 Jim 99 Math
3 Tim 98 Math
4 Joe 99 History
5 Jim 100 History
6 Tim 89 History
7 Joe 80 Geography
8 Tim 100 Geography
9 Jim 99 Geography
我希望结果是:
SELECT Subject, Name, RANK() OVER (PARTITION BY Subject ORDER BY Score DESC)
FROM Table
Subject Name Rank
Math Joe 1
Math Jim 2
History Jim 1
History Joe 2
Geography Tim 1
Geography Jim 2
我只希望每个类别中的排名分别为1和2。我该怎么做呢?
RANK(2) OVER (PARTITION BY Col2 ORDER B Y Col3) AS Top_2_Ranks
。可能会在将来的版本中发生。谢谢你的主意。