带有JOIN和GROUP BY的CASE语句

时间:2015-12-13 22:40:31

标签: sql-server

我正试图通过他们输入的特定比赛从赛事中选出比赛的获胜者。

competition

competition_ID, eventss_ID, competitor_ID, stageName, roundNo, startTime, finisheTime, judges_ID

eventss

 eventss_ID, eventsName, noOfStages, eventsDate, entryFee, venue_ID, judges_ID

我想要的结果是;

Event        RoundNo       competitior_ID     Competiton Winner
swimming       1                COM101            1st Place
swimming       1                COM213            2nd Place
swimming       2                COM101            1st Place
swimming       2                COM234            2nd Place
golf           1                COM654            1st Place
golf           1                COM874            2nd Place

我试过查询:

        SELECT * 

            ,CASE
            WHEN finshTime = (SELECT MIN(finshTime) FROM competition) THEN '1st Place'
            WHEN finshTime = (SELECT MAX(finshTime) FROM competition) THEN '2nd Place'

       ELSE 'Draw'

       END [Competition Winner]

       FROM competition

       JOIN eventss on eventss.eventss_ID = competition.eventss_ID

       GROUP BY competition.roundNo

1 个答案:

答案 0 :(得分:0)

这可能会产生所需的输出,但如果没有适当的样本数据,很难说清楚。

; WITH CTE AS (
    SELECT *
        , ROW_NUMBER() OVER (PARTITION BY eventss_ID, roundNo ORDER BY finishTime) RN
    FROM competition) 
, CTE2 AS (
    SELECT *
        , CASE (SELECT finishTime FROM CTE WHERE RN = 1 AND eventss_ID = c.eventss_ID AND roundNo = c.roundNO) - (SELECT finishTime FROM CTE WHERE RN = 2 AND eventss_ID = c.eventss_ID AND roundNo = c.roundNO)
            WHEN 0 THEN 'Draw'
            ELSE CASE RN 
                WHEN 1 THEN '1st Place' 
                ELSE '2nd Place' END END Drawn
    FROM CTE c
    WHERE RN IN (1, 2))
SELECT e.eventsName [Event], CTE2.RoundNo, CTE2.competitor_ID, CTE2.Drawn [Competition Winner]
FROM eventss e
JOIN CTE2 ON CTE2.eventss_ID = e.eventss_ID
ORDER BY e.eventsName, CTE2.roundNo, CTE2.Drawn

注意:我假设"结束时间"存储为TIME或DATETIME。如果将其存储为其他内容,则无法正常工作。

编辑:如果超过两个人并列第一名,或者第二名并列第二名,则此查询应该有效...

; WITH CTE AS (
    SELECT *
        , ROW_NUMBER() OVER (PARTITION BY eventss_ID, roundNo ORDER BY finishTime) RN
    FROM competition) 
, tiesforfirst AS (
    SELECT *
        , '1st Place' Drawn
    FROM CTE T
    WHERE finishTime = (SELECT finishTime FROM CTE WHERE RN = 1 AND eventss_ID = T.eventss_ID AND roundNo = T.roundNo))
, tiesforsecond AS (
    SELECT *
        , '2nd Place' Drawn
    FROM CTE T
    WHERE finishTime = (SELECT finishTime FROM CTE WHERE RN = 2 AND eventss_ID = T.eventss_ID AND roundNo = T.roundNo)
    AND (SELECT COUNT(*) FROM tiesforfirst WHERE eventss_ID = T.eventss_ID AND roundNo = T.roundNo) = 1)
SELECT e.eventsName [Event], tf.RoundNo, tf.competitor_ID
    , CASE (SELECT COUNT(*) FROM tiesforfirst WHERE eventss_ID = tf.eventss_ID AND roundNo = tf.roundNo) WHEN 1 THEN tf.Drawn ELSE 'Drawn 1st' END [Competition Winner]
FROM eventss e
JOIN tiesforfirst tf ON tf.eventss_ID = e.eventss_ID
UNION
SELECT e.eventsName [Event], tf.RoundNo, tf.competitor_ID
    , CASE (SELECT COUNT(*) FROM tiesforsecond WHERE eventss_ID = tf.eventss_ID AND roundNo = tf.roundNo) WHEN 1 THEN tf.Drawn ELSE 'Drawn 2nd' END [Competition Winner]
FROM eventss e
JOIN tiesforsecond tf ON tf.eventss_ID = e.eventss_ID
ORDER BY e.eventsName, roundNo