SQL Pivot 数据到每个项目一行
Posted
技术标签:
【中文标题】SQL Pivot 数据到每个项目一行【英文标题】:SQL Pivot data into one row per item 【发布时间】:2018-10-13 03:47:04 【问题描述】:我有一个这样的表,它的每个 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】:条件聚合更好地完成多列数据透视:
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 |
+---------+-------+----------+-------+----------+-------+----------+-------+----------+
【讨论】:
以上是关于SQL Pivot 数据到每个项目一行的主要内容,如果未能解决你的问题,请参考以下文章