Sql Server、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;
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
本文地址:/shujuku/shujukujiqiao/101637.html