有谁知道如何将这个TSQL转换为我需要的PIVOT表?

时间:2013-04-03 08:13:19

标签: sql sql-server sql-server-2008 tsql pivot

我一直试图理解PIVOT thingy在谷歌中我可以抓取的所有结果中的差异方面。我翻译我没有运气。 :(

我想知道我们如何翻译这张表

+-------------+------------------------------+-------------+-------+
|  Question   |          AnswerText          | AnswerText2 | COUNT |
+-------------+------------------------------+-------------+-------+
| Answer this | Please select 'No'           | No          |    13 |
| Answer this | Please select 'Yes'          | No          |     1 |
| Answer this | Please select 'Yes' and 'No' | No          |    12 |
| Answer this | Please select 'Yes'          | Yes         |    13 |
| Answer this | Please select 'Yes' and 'No' | Yes         |    14 |
+-------------+------------------------------+-------------+-------+

我在上表中使用此TSQL

    SELECT q.QuestionText as Question, a.AnswerText, pa.AnswerText2, COUNT  ( pa.AnswerText2 ) COUNT
FROM Answer a
    JOIN Question q on a.QuestionId = q.QuestionId
    LEFT JOIN ParticipantAnswer pa on pa.AnswerId = a.AnswerId
    LEFT JOIN Participant p on p.ParticipantId = pa.ParticipantId
WHERE q.QuestionId = 103
            AND a.AnswerTypeId = 2

GROUP BY  q.QuestionText, a.AnswerText, pa.AnswerText2

这样的东西
+--------------------------------------------+---------------------+
|   Question  |          AnswerText          | YES         | NO    |
+--------------------------------------------+---------------------+
| Answer this | Please select 'No'           | 0           |    13 |
| Answer this | Please select 'Yes'          | 13          |     1 |
| Answer this | Please select 'Yes' and 'No' | 14          |    12 |
+--------------------------------------------+---------------------+

我希望有些人可以帮助我。 :)

非常感谢!

最诚挚的问候!

2 个答案:

答案 0 :(得分:2)

SELECT  [Question], 
        [AnswerText], 
        COALESCE(pivotedData.Yes, 0) [Yes],
        COALESCE(pivotedData.No, 0) [N0]
FROM
        (
            SELECT  [Question], [AnswerText], [AnswerText2], [COUNT]
            FROM    TableName
        ) origData
        PIVOT
        (
            MAX([COUNT])
            FOR [AnswerText2] IN ([Yes], [No])
        ) pivotedData;

输出

╔═════════════╦══════════════════════════════╦═════╦════╗
║  QUESTION   ║          ANSWERTEXT          ║ YES ║ N0 ║
╠═════════════╬══════════════════════════════╬═════╬════╣
║ Answer this ║ Please select 'No'           ║   0 ║ 13 ║
║ Answer this ║ Please select 'Yes'          ║  13 ║  1 ║
║ Answer this ║ Please select 'Yes' and 'No' ║  14 ║ 12 ║
╚═════════════╩══════════════════════════════╩═════╩════╝

在不使用PIVOT关键字的情况下产生相同输出的另一个解决方案是使用MAX()CASE()

SELECT  [Question],
        [AnswerText],
        MAX(CASE WHEN [AnswerText2] = 'Yes' THEN [COUNT] END) 'Yes',
        MAX(CASE WHEN [AnswerText2] = 'No' THEN [COUNT] END) 'No'
FROM    TableName
GROUP   BY  [Question],
            [AnswerText]

更新1

SELECT  [Question], 
        [AnswerText], 
        COALESCE(pivotedData.Yes, 0) [Yes],
        COALESCE(pivotedData.No, 0) [N0]
FROM
        (
            SELECT  q.QuestionText as Question, 
                    a.AnswerText, 
                    pa.AnswerText2, 
                    COUNT(pa.AnswerText2) COUNT
            FROM    Answer a
                    INNER JOIN Question q on a.QuestionId = q.QuestionId
                    LEFT JOIN ParticipantAnswer pa on pa.AnswerId = a.AnswerId
                    LEFT JOIN Participant p on p.ParticipantId = pa.ParticipantId
            WHERE   q.QuestionId = 103 AND 
                    a.AnswerTypeId = 2
            GROUP   BY  q.QuestionText, 
                        a.AnswerText, 
                        pa.AnswerText2
        ) origData
        PIVOT
        (
            MAX([COUNT])
            FOR [AnswerText2] IN ([Yes], [No])
        ) pivotedData;

答案 1 :(得分:0)

试试这个:

SELECT *
FROM
(
  -- Put Yourquery here
) AS t
PIVOT
(
  MAX([Count])
  FOR AnswerText2 IN ([Yes], [No])
) AS p;

SQL Fiddle Demo