2011-11-15 47 views
5

考虑到这个表:更新,与结果排名

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

我试图写T-SQL确实有EVENTID作为输入,并使用RANK功能由得分排名,但与部分分离(排名个人对于每个部分,等级1为在每个部分中最高得分等),然后设定/更新所述Rank值

回答

11
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。

+3

工作完美,希望我能给你多个投票! – StefanE

+0

@StefanE你的愿望成真:P –