考虑此表:
create table t (EventId int
,Section int
,PlayerName nvarchar(50)
,Score int
,Rank int
)
我尝试编写具有EventId作为输入的T-SQL,并使用RANK
函数按分数排名,但将各部分分开(每个部分的个人排名,每个部分的最高分数排名1)部分等)然后设置/更新Rank值
答案 0 :(得分:12)
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它正在SEDE上运行。