(编辑:jimmy 日期: 2024/12/28 浏览:2)
下面我们分2种情况,来写出数据排名的实现过程。测试数据如下:
排名后的结果如下:
Access
复制代码 代码如下:
select name, score, (select iif(isnull(sum(1)), 1, sum(1) + 1) from score_rank where score > a.score) as rank from score_rank a order by score desc
sqlserver
复制代码 代码如下:
select name, score, (select ISNULL(sum(1),0) + 1 from score_rank where score > a.score) as rank from score_rank a order by score desc
对于 SQL SERVER 2005 及更高版本
复制代码 代码如下:
SELECT name, score, RANK() OVER (ORDER BY score DESC) AS [rank], DENSE_RANK() OVER (ORDER BY score DESC) AS [rank1], NTILE(4) OVER(ORDER BY score DESC) AS [rank2] FROM score_rank ORDER BY score DESC;