【问题标题】:SQL Pivot when column data is in single column with a discriminator column当列数据位于带有鉴别器列的单列中时,SQL Pivot
【发布时间】:2016-11-16 21:41:26
【问题描述】:

我有一组数据代表人们如何对各种特征进行排名。我试图将数据按两列进行透视,但是,列数据包含在单个列下,单独的列充当鉴别器。

这里是场景。人们得到一份包含三个特征的列表,并要求他们按照对他们的重要性排序。他们获得了两个列表,每个列表包含三个特征,并要求他们将每个列表从最重要到最不重要排列为 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 上进行 Pivot,但我无法弄清楚当数据位于由鉴别器列 (QuestionGroup) 分隔的同一列 (QuestionNumber) 中时如何进行 Pivot。

我还创建了一个在线表here

【问题讨论】:

    标签: sql sql-server pivot pivot-table sql-server-2014


    【解决方案1】:

    我认为最简单的透视方法是条件聚合:

    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;
    

    【讨论】:

    • 我想我被 PIVOT 关键字迷住了,以至于忘了回到基础。
    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-15
    相关资源
    最近更新 更多