Sql Server、Access数据排名的实现方法(例如:成绩排
编程学习 2021-07-05 16:49www.dzhlxh.cn编程入门
在很多时候,我们需要统计数据的排名情况,最常见的是成绩、投票数等等的。
但是,在SQL SERVER 2005 之前,SQL SERVER 2000 并没有提供这个直接的函数供我们使用,同样 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;
狼蚁网站SEO优化我们分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;