将行转换为单行
convert lines into single row
我有这样的数据
Col1 Col 2
100 1
100 2
100 3
100 4
需要这样的输出
Col1 col2 col3 col4 col5
100 1 2 3 4
需要定义新的列名称并将值填充到这些字段中。
如果您有固定的最大输入 rows/output 列数,您可以使用:
SELECT *
FROM table_name
PIVOT (
MAX(Col2)
FOR Col2 IN (1 AS col2, 2 AS col3, 3 AS col4, 4 AS col5)
)
其中,对于示例数据:
CREATE TABLE table_name (Col1, Col2) AS
SELECT 100, 1 FROM DUAL UNION ALL
SELECT 100, 2 FROM DUAL UNION ALL
SELECT 100, 3 FROM DUAL UNION ALL
SELECT 100, 4 FROM DUAL;
输出:
COL1
COL2
COL3
COL4
COL5
100
1
2
3
4
如果最多可以有 15 行,则:
SELECT *
FROM (
SELECT t.*, ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY Col2) AS rn
FROM table_name t
)
PIVOT (
MAX(Col2)
FOR rn IN (
1 AS col2,
2 AS col3,
3 AS col4,
4 AS col5,
5 AS col6,
6 AS col7,
7 AS col8,
8 AS col9,
9 AS col10,
10 AS col11,
11 AS col12,
12 AS col13,
13 AS col14,
14 AS col15,
15 AS col16
)
)
或:
SELECT Col1,
MAX(CASE rn WHEN 1 THEN Col2 END) AS Col2,
MAX(CASE rn WHEN 2 THEN Col2 END) AS Col3,
MAX(CASE rn WHEN 3 THEN Col2 END) AS Col4,
MAX(CASE rn WHEN 4 THEN Col2 END) AS Col5,
MAX(CASE rn WHEN 5 THEN Col2 END) AS Col6,
MAX(CASE rn WHEN 6 THEN Col2 END) AS Col7,
MAX(CASE rn WHEN 7 THEN Col2 END) AS Col8,
MAX(CASE rn WHEN 8 THEN Col2 END) AS Col9,
MAX(CASE rn WHEN 9 THEN Col2 END) AS Col10,
MAX(CASE rn WHEN 10 THEN Col2 END) AS Col11,
MAX(CASE rn WHEN 11 THEN Col2 END) AS Col12,
MAX(CASE rn WHEN 12 THEN Col2 END) AS Col13,
MAX(CASE rn WHEN 13 THEN Col2 END) AS Col14,
MAX(CASE rn WHEN 14 THEN Col2 END) AS Col15,
MAX(CASE rn WHEN 15 THEN Col2 END) AS Col16
FROM (
SELECT t.*, ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY Col2) AS rn
FROM table_name t
)
GROUP BY Col1
这两个输出:
COL1
COL2
COL3
COL4
COL5
COL6
COL7
COL8
COL9
COL10
COL11
COL12
COL13
COL14
COL15
COL16
100
1
2
3
4
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
NULL
db<>fiddle here
还有一种方法,称为条件聚合来实现相同的结果-
SELECT Col1,
MAX(CASE WHEN Col2 = 1 THEN Col2 END) Col2,
MAX(CASE WHEN Col2 = 2 THEN Col2 END) Col3,
MAX(CASE WHEN Col2 = 3 THEN Col2 END) Col4,
MAX(CASE WHEN Col2 = 4 THEN Col2 END) Col5
FROM table_name
GROUP BY Col1;
如果您有 15 个值,您必须在两个查询中手动传递它们,您的结果将始终有 15 列。如果你想让它们动态转换,请参考原评论中的答案。
我有这样的数据
Col1 Col 2
100 1
100 2
100 3
100 4
需要这样的输出
Col1 col2 col3 col4 col5
100 1 2 3 4
需要定义新的列名称并将值填充到这些字段中。
如果您有固定的最大输入 rows/output 列数,您可以使用:
SELECT *
FROM table_name
PIVOT (
MAX(Col2)
FOR Col2 IN (1 AS col2, 2 AS col3, 3 AS col4, 4 AS col5)
)
其中,对于示例数据:
CREATE TABLE table_name (Col1, Col2) AS
SELECT 100, 1 FROM DUAL UNION ALL
SELECT 100, 2 FROM DUAL UNION ALL
SELECT 100, 3 FROM DUAL UNION ALL
SELECT 100, 4 FROM DUAL;
输出:
COL1 COL2 COL3 COL4 COL5 100 1 2 3 4
如果最多可以有 15 行,则:
SELECT *
FROM (
SELECT t.*, ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY Col2) AS rn
FROM table_name t
)
PIVOT (
MAX(Col2)
FOR rn IN (
1 AS col2,
2 AS col3,
3 AS col4,
4 AS col5,
5 AS col6,
6 AS col7,
7 AS col8,
8 AS col9,
9 AS col10,
10 AS col11,
11 AS col12,
12 AS col13,
13 AS col14,
14 AS col15,
15 AS col16
)
)
或:
SELECT Col1,
MAX(CASE rn WHEN 1 THEN Col2 END) AS Col2,
MAX(CASE rn WHEN 2 THEN Col2 END) AS Col3,
MAX(CASE rn WHEN 3 THEN Col2 END) AS Col4,
MAX(CASE rn WHEN 4 THEN Col2 END) AS Col5,
MAX(CASE rn WHEN 5 THEN Col2 END) AS Col6,
MAX(CASE rn WHEN 6 THEN Col2 END) AS Col7,
MAX(CASE rn WHEN 7 THEN Col2 END) AS Col8,
MAX(CASE rn WHEN 8 THEN Col2 END) AS Col9,
MAX(CASE rn WHEN 9 THEN Col2 END) AS Col10,
MAX(CASE rn WHEN 10 THEN Col2 END) AS Col11,
MAX(CASE rn WHEN 11 THEN Col2 END) AS Col12,
MAX(CASE rn WHEN 12 THEN Col2 END) AS Col13,
MAX(CASE rn WHEN 13 THEN Col2 END) AS Col14,
MAX(CASE rn WHEN 14 THEN Col2 END) AS Col15,
MAX(CASE rn WHEN 15 THEN Col2 END) AS Col16
FROM (
SELECT t.*, ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY Col2) AS rn
FROM table_name t
)
GROUP BY Col1
这两个输出:
COL1 COL2 COL3 COL4 COL5 COL6 COL7 COL8 COL9 COL10 COL11 COL12 COL13 COL14 COL15 COL16 100 1 2 3 4 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
db<>fiddle here
还有一种方法,称为条件聚合来实现相同的结果-
SELECT Col1,
MAX(CASE WHEN Col2 = 1 THEN Col2 END) Col2,
MAX(CASE WHEN Col2 = 2 THEN Col2 END) Col3,
MAX(CASE WHEN Col2 = 3 THEN Col2 END) Col4,
MAX(CASE WHEN Col2 = 4 THEN Col2 END) Col5
FROM table_name
GROUP BY Col1;
如果您有 15 个值,您必须在两个查询中手动传递它们,您的结果将始终有 15 列。如果你想让它们动态转换,请参考原评论中的答案。