1

我一直试图在我可以抓取的谷歌所有结果中了解 PIVOT 事物的不同方面。我没有运气翻译我的。:(

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

+-------------+------------------------------+-------------+-------+
|  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 |
+--------------------------------------------+---------------------+

我希望有人能帮助我。:)

非常感谢!

最好的祝福!

4

2 回答 2

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()andCASE()

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;
于 2013-04-03T08:17:55.507 回答
0

试试这个:

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

SQL 小提琴演示

于 2013-04-03T08:18:04.603 回答