在很多时候,我们需要统计数据的排名情况,最常见的是成绩、投票数等等的。但是,在SQL SERVER 2005 之前,SQL SERVER 2000 并没有提供这个直接的函数供我们使用,同样 ACCESS 也是如此。
对于 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
对于 SQL SERVER 2000 及更低版本 :
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;
发表回复