SQL数据透视表上的文本值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL数据透视表上的文本值相关的知识,希望对你有一定的参考价值。

我试图在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
答案

您可以使用命令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
另一答案

您可以尝试以下代码:

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
另一答案

你可以试试这个:

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

以上是关于SQL数据透视表上的文本值的主要内容,如果未能解决你的问题,请参考以下文章

s-s-rS 数据透视表上的条件单元格格式

带有文本值的 SQL Server 数据透视表

SQL:同一表上的多个连接的性能

多对多数据透视表上的自定义范围

将 SQL 代码(作为字符串)转换为表 - 动态数据透视

vbscript 过滤同一工作表上的所有数据透视表