SQL 当列数据位于具有鉴别器列的单列中时进行数据透视
SQL Pivot when column data is in single column with a discriminator column
我有一组数据代表人们如何对各种特征进行排名。我正在尝试按两列对数据进行透视,但是,列数据包含在一个单独的列下,其中一个单独的列充当鉴别器。
这是场景。人们得到了一份包含三个特征的清单,并要求他们按照对他们的重要性进行排序。他们得到了两份清单,每份清单包含三个特征,并要求他们按照从最重要到最不重要的顺序对每个清单进行 1 到 3 的排序。
+----------+-------+----------------+---------------+------+
| RecordNo | Name | QuestionNumber | QuestionGroup | Rank |
+----------+-------+----------------+---------------+------+
| 1 | Bob | 1 | 1 | 2 |
| 2 | Bob | 2 | 1 | 1 |
| 3 | Bob | 3 | 1 | 3 |
| 4 | Bob | 1 | 2 | 1 |
| 5 | Bob | 2 | 2 | 2 |
| 6 | Bob | 3 | 2 | 3 |
| 7 | Sally | 1 | 1 | 3 |
| 8 | Sally | 2 | 1 | 2 |
| 9 | Sally | 3 | 1 | 1 |
| 10 | Sally | 1 | 2 | 1 |
| 11 | Sally | 2 | 2 | 3 |
| 12 | Sally | 3 | 2 | 2 |
+----------+-------+----------------+---------------+------+
我想得到的是数据的 PIVOT,所以它看起来像这样..
+----------+-------+-----------+-----------+-----------+------------+------------+------------+
| RecordNo | Name | Question1 | Question2 | Question3 | Question 1 | Question 2 | Question 3 |
| | | Group 1 | Group 1 | Group 1 | Group 2 | Group 2 | Group 2 |
+----------+-------+-----------+-----------+-----------+------------+------------+------------+
| 1 | Bob | 2 | 1 | 3 | 1 | 2 | 3 |
| 2 | Sally | 3 | 2 | 1 | 1 | 3 | 2 |
+----------+-------+-----------+-----------+-----------+------------+------------+------------+
我知道如何在多个列上进行数据透视 great article on it here 但我无法弄清楚的是当数据位于由鉴别器列 (QuestionGroup) 分隔的同一列 (QuestionNumber) 中时如何进行数据透视。
我还在线创建了一个tablehere
我认为最简单的数据透视方法是条件聚合:
select name,
max(case when questiongroup = 1 and questionnumber = 1 then rank end) as q_1_1,
max(case when questiongroup = 1 and questionnumber = 2 then rank end) as q_1_2,
max(case when questiongroup = 1 and questionnumber = 3 then rank end) as q_1_3,
max(case when questiongroup = 2 and questionnumber = 1 then rank end) as q_2_1,
max(case when questiongroup = 2 and questionnumber = 2 then rank end) as q_2_2,
max(case when questiongroup = 2 and questionnumber = 3 then rank end) as q_2_3
from t
group by name;
我有一组数据代表人们如何对各种特征进行排名。我正在尝试按两列对数据进行透视,但是,列数据包含在一个单独的列下,其中一个单独的列充当鉴别器。
这是场景。人们得到了一份包含三个特征的清单,并要求他们按照对他们的重要性进行排序。他们得到了两份清单,每份清单包含三个特征,并要求他们按照从最重要到最不重要的顺序对每个清单进行 1 到 3 的排序。
+----------+-------+----------------+---------------+------+
| RecordNo | Name | QuestionNumber | QuestionGroup | Rank |
+----------+-------+----------------+---------------+------+
| 1 | Bob | 1 | 1 | 2 |
| 2 | Bob | 2 | 1 | 1 |
| 3 | Bob | 3 | 1 | 3 |
| 4 | Bob | 1 | 2 | 1 |
| 5 | Bob | 2 | 2 | 2 |
| 6 | Bob | 3 | 2 | 3 |
| 7 | Sally | 1 | 1 | 3 |
| 8 | Sally | 2 | 1 | 2 |
| 9 | Sally | 3 | 1 | 1 |
| 10 | Sally | 1 | 2 | 1 |
| 11 | Sally | 2 | 2 | 3 |
| 12 | Sally | 3 | 2 | 2 |
+----------+-------+----------------+---------------+------+
我想得到的是数据的 PIVOT,所以它看起来像这样..
+----------+-------+-----------+-----------+-----------+------------+------------+------------+
| RecordNo | Name | Question1 | Question2 | Question3 | Question 1 | Question 2 | Question 3 |
| | | Group 1 | Group 1 | Group 1 | Group 2 | Group 2 | Group 2 |
+----------+-------+-----------+-----------+-----------+------------+------------+------------+
| 1 | Bob | 2 | 1 | 3 | 1 | 2 | 3 |
| 2 | Sally | 3 | 2 | 1 | 1 | 3 | 2 |
+----------+-------+-----------+-----------+-----------+------------+------------+------------+
我知道如何在多个列上进行数据透视 great article on it here 但我无法弄清楚的是当数据位于由鉴别器列 (QuestionGroup) 分隔的同一列 (QuestionNumber) 中时如何进行数据透视。
我还在线创建了一个tablehere
我认为最简单的数据透视方法是条件聚合:
select name,
max(case when questiongroup = 1 and questionnumber = 1 then rank end) as q_1_1,
max(case when questiongroup = 1 and questionnumber = 2 then rank end) as q_1_2,
max(case when questiongroup = 1 and questionnumber = 3 then rank end) as q_1_3,
max(case when questiongroup = 2 and questionnumber = 1 then rank end) as q_2_1,
max(case when questiongroup = 2 and questionnumber = 2 then rank end) as q_2_2,
max(case when questiongroup = 2 and questionnumber = 3 then rank end) as q_2_3
from t
group by name;