简体   繁体   English

SQL 返回一个项目的排名

[英]SQL return the rank of an item

how to make a query that gets the rank of an item?如何进行查询以获取项目的排名? (for azure database) (对于 azure 数据库)

for example:例如:

table name: studentScore

    studentName Scrore 
    student1      80
    student2      70
    student3      90
    student4      60

want to get the rank of student1, which is 2.想得到student1的rank,也就是2。

what I tried:我尝试了什么:

SELECT ROW_NUMBER() OVER(ORDER BY [Scrore])AS Rank 
FROM studentScore where [name] = 'student1'

return返回

Rank
  1

I want我想

Rank
 2

You can use rank() or dense_rank() functions:您可以使用 rank() 或 dense_rank() 函数:

declare
    @t table (studentName varchar (100), Score int)

  insert into @t
  values 
    ('student1', 80),
    ('student2', 70),
    ('student3', 90),
    ('student4', 60)

    select rank() over (order by Score desc) rnk, * from @t

    select dense_rank() over (order by Score desc) rnk, * from @t

OUTPUT OUTPUT

输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM