SQL数据透视表上的文本值

问题描述 投票:1回答:3

我试图在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
sql sql-server tsql pivot
3个回答
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
投票

您可以尝试以下代码:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.