Python 在SQL中按列值对项进行排序

Python 在SQL中按列值对项进行排序,python,mysql,sql,Python,Mysql,Sql,鉴于下表内容: StudentNum Score 1 100% 2 95% 3 99% 4 0% 5 12% 如何获得StudentNum=3的“五分之二”排名 我能想到的唯一方法是在应用程序代码中,获取所有项目,然后获取索引。例如: items = SELECT StudentNum FROM table ORDER BY score DESC studen

鉴于下表内容:

StudentNum    Score
 1             100%
 2             95%
 3             99%
 4             0%
 5             12%
如何获得StudentNum=3的“五分之二”排名

我能想到的唯一方法是在应用程序代码中,获取所有项目,然后获取索引。例如:

items = SELECT StudentNum FROM table ORDER BY score DESC
student_3_position = items.index('3') + 1
total_positions = len(items)
'Student3 is #%s out of %s' % (student_3_position, total_positions)
有没有一种直接在SQL中执行此操作的方法?

准备数据:

 create table dbo.temp (StudentNum int identity (1,1), Score int)
 insert into dbo.temp values (100)
 insert into dbo.temp values (95)  
 insert into dbo.temp values (99)
 insert into dbo.temp values (12)
 insert into dbo.temp values (0)
结果:

select *, ROW_NUMBER() OVER (ORDER BY Score desc) as 'Rank' from dbo.temp order by StudentNum
请参阅:。