Pivot/Group 按键数据

Pivot/Group data by key

我有一个像这样的 table (survey_source) 可以填充 answer_text 中的任何值或 NULL:

+-----------+---------------+-------------+---------------+------------+
| survey_id | submission_id | question_id | question_text | answer_text|
+-----------+---------------+-------------+---------------+------------+
| 001       | 2             | question1   | why?          | answer A   |
+-----------+---------------+-------------+---------------+------------+
| 001       | 2             | question2   | how?          | answer B   |
+-----------+---------------+-------------+---------------+------------+
| 001       | 5             | question1   | why?          | answer C   |
+-----------+---------------+-------------+---------------+------------+
| 001       | 5             | question2   | how?          | answer D   |
+-----------+---------------+-------------+---------------+------------+
| 001       | 5             | question3   | when?         | answer E   |
+-----------+---------------+-------------+---------------+------------+

我想旋转此数据,使新列为 survey_idsubmission_idquestion1question2question3,这样每个提交是它自己的行 vs 搜索行是它自己的问题每次提交。

如果我使用 CASE WHEN submission_id = 2 THEN answer_text,我可以旋转列,但由于其他值的 NULLS,我仍然得到很多行。

有没有办法得到想要的结果?我能想到的唯一方法是 select 独特的 submission_id,然后每个问题左连接回 table 一次,这似乎有点矫枉过正。

SELECT
    t1.survey_id,
    t1.submission_id,
    t2.answer_text as question1
FROM survey_source t1
LEFT JOIN (
               SELECT
                   submission_id,
                   answer_text
               FROM survey_source
               WHERE question_id = 'question1'
            ) t2
 ON t1.submission_id = t2.submission_id

 --- Repeat join to get question2
 --- Repeat join to get question3

想要的输出 table 像这样:

+-----------+---------------+-----------+-----------+-----------+
| survey_id | submission_id | question1 | question2 | question3 |
+-----------+---------------+-----------+-----------+-----------+
| 001       | 2             | answer A  | answer B  | NULL      |
+-----------+---------------+-----------+-----------+-----------+
| 001       | 5             | answer C  | answer D  | answer E  |
+-----------+---------------+-----------+-----------+-----------+

使用 MAX 的 GROUP BY 是一种实现此目的的方法。

select
    survey_id,
    submission_id,
    max(case when question_id = 'question1' then answer_text end) as 'question1',
    max(case when question_id = 'question2' then answer_text end) as 'question2',
    max(case when question_id = 'question3' then answer_text end) as 'question3'
from survey_source
group by survey_id, submission_id
order by survey_id, submission_id;