SQL数据透视表上的文本值

时间:2018-09-03 09:57:31

标签: sql sql-server tsql pivot

我正在尝试透视SQL中的以下数据集。

SELECT  ParticipantID,
        ContactReasonTypeID,
        Completed = CASE WHEN Returned IS NOT NULL
                        THEN    'Completed'
                        ELSE 'Not completed'
                    END
FROM    ContactReason
WHERE   ContactReasonTypeID = 40 -- M4
UNION
SELECT  ParticipantID,
        ContactReasonTypeID,
        Completed = CASE WHEN Returned IS NOT NULL
                        THEN    'Completed'
                        ELSE 'Not completed'
                    END
FROM    ContactReason
WHERE   ContactReasonTypeID = 80 -- M8

哪个给出以下结果:

ParticipantID | QuestionnaireID  | Completed
1               40                 'Completed'
1               80                 'Not completed'
2               40                 'Completed'
2               80                 'Completed'
3               40                 'Completed'
3               80                 'Not completed'
4               40                 'Completed'
4               80                 'Not completed'

我想要的是这样旋转结果(其中M4是QuestionnaireID 40,M8是80):

ParticipantID | M4           | M8
1               'Completed'    'Not completed'
2               'Completed'    'Completed'
3               'Completed'    'Not completed'
4               'Completed'    'Not completed'

我正在努力如何旋转桌子。我当前的SQL如下,并导致错误。我曾尝试根据其他帖子在各列之间切换,但无法确定需要什么:

SELECT  *
FROM    (
    SELECT  ParticipantID,
            ContactReasonTypeID,
            Completed = CASE WHEN Returned IS NOT NULL
                            THEN    'Completed'
                            ELSE 'Not completed'
                        END
    FROM    ContactReason
    WHERE   ContactReasonTypeID = 40
    UNION
    SELECT  ParticipantID,
            ContactReasonTypeID,
            Completed = CASE WHEN Returned IS NOT NULL
                            THEN    'Completed'
                            ELSE 'Not completed'
                        END
    FROM    ContactReason
    WHERE   ContactReasonTypeID = 80
) AS tbl
PIVOT (
    MAX(Returned)
    FOR ContactReasonTypeID IN ([M4],[M8])
) AS pvt
ORDER BY ParticipantID

3 个答案:

答案 0 :(得分:3)

您可以为此使用命令PIVOT

DECLARE @t table(ParticipantID INT, QuestionnaireID INT, Completed varchar(20))
INSERT @t values
(1,40,'Completed'),(1,80,'Not completed'),(2,40,'Completed'),
(2,80,'Completed'),(3,40,'Completed'),(3,80,'Not completed'),
(4,40,'Completed'),(4,80,'Not completed')


SELECT ParticipantID, [40] M4, [80] M8
FROM @t
PIVOT
  (min(COMPLETED)  
FOR QuestionnaireID
  in([40],[80])  
)AS p
ORDER BY ParticipantID

结果:

ParticipantID   M4          M8
1               Completed   Not completed
2               Completed   Completed
3               Completed   Not completed
4               Completed   Not completed

答案 1 :(得分:1)

您可以尝试以下代码:

SELECT  DISTINCT CR.ParticipantID,
        (SELECT CASE
                    WHEN Returned IS NOT NULL
                    THEN 'Completed'
                    ELSE 'Not completed'
                END M4
           FROM ContactReason
          WHERE ContactReasonTypeID = 40 --M4
            AND ParticipantID = CR.ParticipantID) M4,
        (SELECT CASE
                    WHEN Returned IS NOT NULL
                    THEN 'Completed'
                    ELSE 'Not completed'
                END M4
           FROM ContactReason
          WHERE ContactReasonTypeID = 80 --M8
            AND ParticipantID = CR.ParticipantID) M8
FROM    ContactReason CR
WHERE   CR.ContactReasonTypeID IN (40, 80) -- M4 and M8

答案 2 :(得分:0)

您可以尝试以下方法:

SELECT q.ParticipantID, 
                  CASE WHEN min(q.QuestionnaireID) = 40
                        THEN min(q.Completed)
                    END as M4,
                   CASE WHEN max(q.QuestionnaireID) = 80
                        THEN max(q.Completed)
                    END as M8
  FROM
(
SELECT  ParticipantID,
        ContactReasonTypeID,
        Completed = CASE WHEN Returned IS NOT NULL
                        THEN    'Completed'
                        ELSE 'Not completed'
                    END
FROM    ContactReason
WHERE   ContactReasonTypeID = 40 -- M4
UNION
SELECT  ParticipantID,
        ContactReasonTypeID,
        Completed = CASE WHEN Returned IS NOT NULL
                        THEN    'Completed'
                        ELSE 'Not completed'
                    END
FROM    ContactReason
WHERE   ContactReasonTypeID = 80
) q
GROUP BY ParticipantID;

Demo内,将内部查询q视为tab2