ID排名 id是有区别的
排名 名字 分数
1 aa100
2bb99
3cc98
3 dd98
4ee97
5ff96
排名 名字 分数
1 aa100
2bb99
3cc98
4 dd98
5ee97
[解决办法]
- 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