これは、任意の数のジャッジで機能する動的ピボット クエリです。ピボット コードはhttps://stackoverflow.com/a/12505138/3574819から適応されます。
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX),
@cols2 AS NVARCHAR(MAX),
@cols3 AS NVARCHAR(MAX)
select @cols = STUFF((SELECT distinct ', ' + QUOTENAME(JudgeID)
from Score
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
select @cols2 = STUFF((SELECT distinct '+' + QUOTENAME('rn' + CONVERT(varchar(1),JudgeID))
from Score
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
select @cols3 = STUFF((SELECT distinct ', ' + QUOTENAME(JudgeID) + ', rank() over (order by ' + QUOTENAME(JudgeId) + ' desc) ' + QUOTENAME('rn' + CONVERT(varchar(1),JudgeID))
from Score
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT * ,
rank() over (order by ' + @cols2 + ') rn,
( ' + @cols2 + ' ) fn
FROM (SELECT ContestantID,' + @cols3 + ' from
(
select ContestantID, ScorePoints, JudgeID
from Score
) x
pivot
(
max(ScorePoints)
for JudgeID in (' + @cols + ')
) p ) t1'
execute(@query)
http://www.sqlfiddle.com/#!6/87b45/1