1

SQLクエリを使用してこの種の結果を得るには、どのような方法/方法が考えられますか。 ここに画像の説明を入力

審査員のスコアを各競技者にランク付けし、ファイナリストを並べ替えて最終順位を示します。

この@Prince Jeaは、解決策を与えられましたが、正しいとはいえ、私が期待していた実際の結果ではありません。それでも、必要な出力を探しています。

SQL クエリ

    SELECT ContestantID,JudgeID,ScorePoints, 
    RANK() OVER (ORDER BY ScorePoints DESC) AS xRank,
    DENSE_RANK() OVER (ORDER BY ScorePoints DESC) AS fRank
FROM 
( 
SELECT ContestantID , ScorePoints, JudgeID
FROM Score
) AS a
ORDER BY 1

結果

ここに画像の説明を入力

SQL Fiddle http://www.sqlfiddle.com/#!6/30d03/2のいくつかの資料を好みます

4

2 に答える 2

1

最初の CTE はジャッジ列で PIVOT を実行しています

2 番目の CTE は、審査員のスコアに基づいて各競技者のランクを計算しています

3 番目の CTE は、各競技者の最終スコアを計算しています

  ; WITH CTE 
    AS
    (
    SELECT ContestantID,
           MAX(CASE when JudgeID =1 THEN ScorePoints END ) as [JudgeID#1],
           MAX(CASE when JudgeID =2 THEN ScorePoints END ) as [JudgeID#2],
           MAX(CASE when JudgeID =3 THEN ScorePoints END ) as [JudgeID#3]
    FROM Score
    Group by ContestantID
    )
    ,  CTE2 AS
    (
    SELECT ContestantID,
           [JudgeID#1],
           RANK() OVER   ( Order by JudgeID#1 desc ) as 'Rank#1',       
           [JudgeID#2],
           RANK() OVER   ( Order by JudgeID#2 desc ) as 'Rank#2',       
           [JudgeID#3],
           RANK() OVER   ( Order by JudgeID#3 desc ) as 'Rank#3'

    FROM CTE
    )
    , CTE3 as
    (
    SELECT ContestantID,
          SUM (Rank#1+ Rank#2 + Rank#1) as total
    FROM CTE2
    GROUP BY ContestantID
    )
    SELECT CTE2.*, CTE3.total as 'Final Score'
    FROM  CTE2
    JOIN CTE3 
    ON CTE2.ContestantID = CTE3.ContestantID
    ORDER BY ContestantID
于 2014-09-30T18:17:26.800 に答える
1

これは、任意の数のジャッジで機能する動的ピボット クエリです。ピボット コードは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

于 2014-09-30T19:06:18.650 に答える