但是,在SQL SERVER 2005 之前,SQL SERVER 2000 并没有提供这个直接的函数供我们使用,同样 ACCESS 也是如此。
下面我们分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;