Rank Result Update

Given this table:

create table t (EventId    int
               ,Section    int
               ,PlayerName nvarchar(50)
               ,Score      int
               ,Rank       int
               )

I try to write T-SQL that has EventId and use the function RANKto rank by score but with divided sections (each rating for each section, Rank 1 for the highest score in each section, and so on), and then install / update rank values

+5
source share
1 answer
UPDATE tbl
SET [Rank] = t2.[Rank]
FROM tbl t1
LEFT OUTER JOIN 
(
  SELECT EventId
  , Section
  , PlayerName
  , Score
  , RANK() OVER (PARTITION BY EventId, Section ORDER BY Score desc) as [Rank]
  FROM tbl
) as t2 
  ON t1.EventId = t2.EventId 
  AND t1.Section = t2.Section
  AND t1.PlayerName = t2.PlayerName

Here he works for SEDE.

+11
source

All Articles