SQL Server: Label Position

I am using SQL Server 2008. I have a Student table that has the following fields: 1. StudentId, 2. StudentName, 3. Characters. I want to get a result set that should have a column named Position. Something like "Select StudentId, StudentName, Marks, as Position from Student ...", so that, depending on the points the student marked, I can rate them as 1, 2 or 20 positions. If the students have the same signs, then they have the same position. Thank you Reese

+3
source share
2 answers

Use RANK :

, . , - SalesYTD, . SalesYTD , , . RANK .

:

SELECT
    StudentId,
    StudentName,
    Marks,
    RANK() OVER (ORDER BY Marks DESC) AS Position
FROM Student
+7

. , :

select
  s.StudentId, s.StudentName, s.Marks,
  Position = 1 + (select count(*) from Students m where m.Marks > s.Marks)
from
  Students s
+1

Source: https://habr.com/ru/post/1744226/


All Articles