多个枢轴

时间:2018-06-18 16:03:35

标签: sql-server tsql pivot

我的表格如下所示:

+---------+----------+---------+-------+-----------+-----------+-------------+
| ValueId | ObjectId |  Field  | Value | Estimated | OrigValue | FromDefault |
+---------+----------+---------+-------+-----------+-----------+-------------+
|       1 |        1 | 'Stat1' | 35    | true      | (null)    | (null)      |
|       2 |        1 | 'Stat2' | 2     | false     | 0         | true        |
|       3 |        1 | 'Stat3' | 0.213 | true      | 0.212     | false       |
|       4 |        2 | 'Stat1' | 513   | true      | 122       | true        |
|       5 |        2 | 'Stat2' | 31    | true      | (null)    | true        |
|       6 |        2 | 'Stat3' | 2.411 | true      | (null)    | false       |
+---------+----------+---------+-------+-----------+-----------+-------------+

小提琴:http://www.sqlfiddle.com/#!9/445271/2/0

我希望枢轴看起来像这样:

+----------+-------+-------+-------+-----------------+-----------------+-------------------+-----------------+-----------------+-------------------+-----------------+-----------------+-------------------+
| ObjectId | Stat1 | Stat2 | Stat3 | Stat1_Estimated | Stat1_OrigValue | Stat1_FromDefault | Stat2_Estimated | Stat2_OrigValue | Stat2_FromDefault | Stat3_Estimated | Stat3_OrigValue | Stat3_FromDefault |
+----------+-------+-------+-------+-----------------+-----------------+-------------------+-----------------+-----------------+-------------------+-----------------+-----------------+-------------------+
|        1 |    35 |     2 | 0.213 | true            | (null)          | (null)            | false           | false           | true              | true            | 0.212           | false             |
|        2 |   513 |    31 | 2.411 | true            | 122             | true              | true            | (null)          | true              | true            | (null)          | false             |
+----------+-------+-------+-------+-----------------+-----------------+-------------------+-----------------+-----------------+-------------------+-----------------+-----------------+-------------------+

小提琴:http://www.sqlfiddle.com/#!9/6e84ff2/2/0

我理解如何使用轴来获取“值”作为字段,但不知道如何包含多个枢轴并将它们命名为Field + _ + OriginalColumn

编辑:Field的不同值的数量众所周知,可以硬编码到答案中。

1 个答案:

答案 0 :(得分:0)

<强> SQL DEMO

因为字段是已知的,所以您可以使用条件聚合函数

SELECT ObjectId,
       MAX(CASE WHEN Field = '''Stat1''' THEN Value END)  as Stat1,
       MAX(CASE WHEN Field = '''Stat2''' THEN Value END)  as Stat2,
       MAX(CASE WHEN Field = '''Stat3''' THEN Value END)  as Stat3,
       MAX(CASE WHEN Field = '''Stat1''' THEN Estimated END)  as Stat_Estimated1,
       MAX(CASE WHEN Field = '''Stat1''' THEN OrigValue END)  as Stat_OrigValue1,
       MAX(CASE WHEN Field = '''Stat1''' THEN FromDefault END)  as Stat_FromDefault1,
       MAX(CASE WHEN Field = '''Stat2''' THEN Estimated END)  as Stat_Estimated2,
       MAX(CASE WHEN Field = '''Stat2''' THEN OrigValue END)  as Stat_OrigValue2,
       MAX(CASE WHEN Field = '''Stat2''' THEN FromDefault END)  as Stat_FromDefault2,
       MAX(CASE WHEN Field = '''Stat3''' THEN Estimated END)  as Stat_Estimated3,
       MAX(CASE WHEN Field = '''Stat3''' THEN OrigValue END)  as Stat_OrigValue3,
       MAX(CASE WHEN Field = '''Stat3''' THEN FromDefault END)  as Stat_FromDefault3

FROM mytable
GROUP BY ObjectId
;

您也可以安排数据并使用PIVOT功能。请注意,value列只能包含一种数据类型,因此我将boolean转换为0/1

<强> SQL DEMO

SELECT ObjectId, Field, Value
FROM myTable

UNION ALL

SELECT ObjectId, CONCAT(Field, '_Estimated'), CASE WHEN Estimated IS NULL THEN NULL
                                                   WHEN Estimated THEN 1
                                                   ELSE 0 
                                              END
FROM myTable

UNION ALL

SELECT ObjectId, CONCAT(Field, '_OrigValue'), OrigValue
FROM myTable

UNION ALL

SELECT ObjectId, CONCAT(Field, '_FromDefault'), CASE WHEN FromDefault IS NULL THEN NULL
                                                     WHEN FromDefault THEN 1
                                                     ELSE 0 
                                                END
FROM myTable
;