排名 名字 分数
1 aa 100
2 bb 99
3 cc 98
3 dd 98
4 ee 97
5 ff 96
排名 名字 分数
1 aa 100
2 bb 99
3 cc 98
4 dd 98
5 ee 97
------解决方案--------------------
- SQL code
--> 测试数据:[test]if object_id('[test]') is not null drop table [test]create table [test]([排名] int,[名字] varchar(2),[分数] int)insert [test]select 1,'aa',100 union allselect 2,'bb',99 union allselect 3,'cc',98 union allselect 3,'dd',98 union allselect 4,'ee',97 union allselect 5,'ff',96select * from(select [排名]=ROW_NUMBER()over(order by [分数] desc),[名字],[分数] from test)twhere [排名]<=(select MAX([排名]) from test)/*排名 名字 分数1 aa 1002 bb 993 cc 984 dd 985 ee 97*/
------解决方案--------------------
- SQL code
select [排名]=ROW_NUMBER()over(order by [分数] desc),[名字],[分数] from test
------解决方案--------------------
1.RANK() OVER()——重复,不连续,1,1,3
2.DENSE_RANK() OVER()——重复,连续,1,1,2
3.ROW_NUMBER() OVER()——不重复,连续,1,2,3