SQL将每个项目的数据透视为一行

时间:2018-05-02 22:59:50

标签: sql-server tsql pivot ssms conditional-aggregation

我有一个这样的表格,每个LAY都有多次指定产品名称:

  NAME   |  LAYER  |  TYPE  |  DEPTH
-------------------------------------
32_42_1  | LAY_1   |   A    |  99.4
32_42_1  | LAY_2   |   D    |  427.2
32_42_1  | LAY_3   |   X    |  120.4
32_42_1  | LAY_4   |   B    |  27
32_42_2  | LAY_1   |   A    |  150.4
32_42_2  | LAY_2   |   D    |  427.2
32_42_2  | LAY_3   |   X    |  121.4
32_42_2  | LAY_4   |   C    |  40
32_42_3  | LAY_1   |   F    |  80.97
32_42_3  | LAY_2   |   Y    |  300.2
32_42_3  | LAY_3   |   C    |  123.4
32_42_3  | LAY_4   |   C    |  120

我需要每个NAME只有一行,因此需要像这样旋转:

 NAME  | LAY_1_TYPE | LAY_1_DEPTH | LAY_2_TYPE | LAY_2_DEPTH | LAY_3...
--------------------------------------------------------------------
32_42_1|    A       |    99.4     |      D     |   427.2     | ...
32_42_2|    A       |    150.4    |      D     |   427.2     | ...
32_42_3|    F       |    80.7     |      Y     |   300.2     | ...

我找到了许多类似的解决方案,虽然其中一些已经接近,但我无法更改代码以满足我的需求。

这是我到目前为止所做的:

PIVOT
(
    MAX(TYPE) For LAYER In (LAY_1,LAY_2,LAY_3,LAY_4,LAY_5)
) piv

但这仅限于一列,并且不会根据“名称”将数据压缩成一行

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

使用条件聚合更好地完成具有多个列的透视:

DECLARE @tbl TABLE (NAME VARCHAR(100),LAYER VARCHAR(100),TYPE VARCHAR(100),DEPTH DECIMAL(10,4));
INSERT INTO @tbl VALUES
 ('32_42_1','LAY_1','A','99.4')
,('32_42_1','LAY_2','D','427.2')
,('32_42_1','LAY_3','X','120.4')
,('32_42_1','LAY_4','B','27')
,('32_42_2','LAY_1','A','150.4')
,('32_42_2','LAY_2','D','427.2')
,('32_42_2','LAY_3','X','121.4')
,('32_42_2','LAY_4','C','40')
,('32_42_3','LAY_1','F','80.97')
,('32_42_3','LAY_2','Y','300.2')
,('32_42_3','LAY_3','C','123.4')
,('32_42_3','LAY_4','C','120');

SELECT t.NAME
      ,MAX(CASE WHEN t.LAYER='LAY_1' THEN t.TYPE END) AS Type1
      ,MAX(CASE WHEN t.LAYER='LAY_1' THEN t.DEPTH END) AS Depth1
      ,MAX(CASE WHEN t.LAYER='LAY_2' THEN t.TYPE END) AS Type2
      ,MAX(CASE WHEN t.LAYER='LAY_2' THEN t.DEPTH END) AS Depth2
      ,MAX(CASE WHEN t.LAYER='LAY_3' THEN t.TYPE END) AS Type3
      ,MAX(CASE WHEN t.LAYER='LAY_3' THEN t.DEPTH END) AS Depth3
      ,MAX(CASE WHEN t.LAYER='LAY_4' THEN t.TYPE END) AS Type4
      ,MAX(CASE WHEN t.LAYER='LAY_4' THEN t.DEPTH END) AS Depth4
FROM @tbl AS t
GROUP BY t.NAME;

结果

+---------+-------+----------+-------+----------+-------+----------+-------+----------+
| NAME    | Type1 | Depth1   | Type2 | Depth2   | Type3 | Depth3   | Type4 | Depth4   |
+---------+-------+----------+-------+----------+-------+----------+-------+----------+
| 32_42_1 | A     | 99.4000  | D     | 427.2000 | X     | 120.4000 | B     | 27.0000  |
+---------+-------+----------+-------+----------+-------+----------+-------+----------+
| 32_42_2 | A     | 150.4000 | D     | 427.2000 | X     | 121.4000 | C     | 40.0000  |
+---------+-------+----------+-------+----------+-------+----------+-------+----------+
| 32_42_3 | F     | 80.9700  | Y     | 300.2000 | C     | 123.4000 | C     | 120.0000 |
+---------+-------+----------+-------+----------+-------+----------+-------+----------+