如何根据公共条目将数据库 table 行移动到列中
How to move database table rows into columns based on a common entry
我有以下数据库:
这只是一个更大的数据集的一个子集,该数据集太大而无法输入。
ID
CODE
IDENTIFIERS
COLORS
SHAPES
NUMB
101
24
N
RED
CIRCLE
174
102
32
N
BLUE
SQUARE
155
102
32
N
PURPLE
STAR
223
103
13
Y
RED
SQUARE
143
103
13
Y
GREEN
CIRCLE
123
103
13
Y
BLUE
SQUARE
142
我需要以下结果:
ID
CODE
IDENTIFIERS
COLORS1
COLORS2
COLORS3
SHAPES1
SHAPES2
SHAPES3
NUMB1
NUMB2
NUMB3
101
24
N
RED
CIRCLE
174
102
32
N
BLUE
PURPLE
SQUARE
STAR
155
223
103
13
Y
RED
GREEN
BLUE
SQUARE
CIRCLE
SQUARE
143
123
142
我试过了:
SELECT ID,
CODE,
IDENTIFIERS,
group_concat(COLORS) AS COLORSS,
group_concat(SHAPES) AS SHAPESS,
group_concat(NUMB) AS NUMBS
FROM (
SELECT a.ID,
a.CODE,
a.IDENTIFIERS,
a.COLORS,
a.SHAPES,
a.NUMB
FROM database AS a
) AS sub
GROUP BY ID, CODE, IDENTIFIERS
我得到了:
ID
CODE
IDENTIFIERS
COLORS
SHAPES
NUMB
101
24
N
RED
CIRCLE
174
102
32
N
BLUE,PURPLE
SQUARE,STAR
155,223
103
13
Y
RED,GREEN,BLUE
SQUARE,CIRCLE,SQUARE
143,123,142
这很接近,但不是我想要的。任何帮助将不胜感激。
首先,您需要一种方法来确定要将行放入哪些列。为此,您可以使用 ROW_NUMBER()
然后您可以使用 MAX(CASE)
的组合来旋转数据。
WITH
sorted AS
(
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY id, code, identifiers) AS row_id
FROM
yourTable
)
SELECT
id,
code,
identifiers,
MAX(CASE WHEN row_id = 1 THEN colors END) AS colors_1,
MAX(CASE WHEN row_id = 1 THEN shapes END) AS shapes_1,
MAX(CASE WHEN row_id = 1 THEN numb END) AS numb_1,
MAX(CASE WHEN row_id = 2 THEN colors END) AS colors_2,
MAX(CASE WHEN row_id = 2 THEN shapes END) AS shapes_2,
MAX(CASE WHEN row_id = 2 THEN numb END) AS numb_2,
MAX(CASE WHEN row_id = 3 THEN colors END) AS colors_3,
MAX(CASE WHEN row_id = 3 THEN shapes END) AS shapes_3,
MAX(CASE WHEN row_id = 3 THEN numb END) AS numb_3
FROM
sorted
GROUP BY
id,
code,
identifiers
我有以下数据库:
这只是一个更大的数据集的一个子集,该数据集太大而无法输入。
ID | CODE | IDENTIFIERS | COLORS | SHAPES | NUMB |
---|---|---|---|---|---|
101 | 24 | N | RED | CIRCLE | 174 |
102 | 32 | N | BLUE | SQUARE | 155 |
102 | 32 | N | PURPLE | STAR | 223 |
103 | 13 | Y | RED | SQUARE | 143 |
103 | 13 | Y | GREEN | CIRCLE | 123 |
103 | 13 | Y | BLUE | SQUARE | 142 |
我需要以下结果:
ID | CODE | IDENTIFIERS | COLORS1 | COLORS2 | COLORS3 | SHAPES1 | SHAPES2 | SHAPES3 | NUMB1 | NUMB2 | NUMB3 |
---|---|---|---|---|---|---|---|---|---|---|---|
101 | 24 | N | RED | CIRCLE | 174 | ||||||
102 | 32 | N | BLUE | PURPLE | SQUARE | STAR | 155 | 223 | |||
103 | 13 | Y | RED | GREEN | BLUE | SQUARE | CIRCLE | SQUARE | 143 | 123 | 142 |
我试过了:
SELECT ID,
CODE,
IDENTIFIERS,
group_concat(COLORS) AS COLORSS,
group_concat(SHAPES) AS SHAPESS,
group_concat(NUMB) AS NUMBS
FROM (
SELECT a.ID,
a.CODE,
a.IDENTIFIERS,
a.COLORS,
a.SHAPES,
a.NUMB
FROM database AS a
) AS sub
GROUP BY ID, CODE, IDENTIFIERS
我得到了:
ID | CODE | IDENTIFIERS | COLORS | SHAPES | NUMB |
---|---|---|---|---|---|
101 | 24 | N | RED | CIRCLE | 174 |
102 | 32 | N | BLUE,PURPLE | SQUARE,STAR | 155,223 |
103 | 13 | Y | RED,GREEN,BLUE | SQUARE,CIRCLE,SQUARE | 143,123,142 |
这很接近,但不是我想要的。任何帮助将不胜感激。
首先,您需要一种方法来确定要将行放入哪些列。为此,您可以使用 ROW_NUMBER()
然后您可以使用 MAX(CASE)
的组合来旋转数据。
WITH
sorted AS
(
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY id, code, identifiers) AS row_id
FROM
yourTable
)
SELECT
id,
code,
identifiers,
MAX(CASE WHEN row_id = 1 THEN colors END) AS colors_1,
MAX(CASE WHEN row_id = 1 THEN shapes END) AS shapes_1,
MAX(CASE WHEN row_id = 1 THEN numb END) AS numb_1,
MAX(CASE WHEN row_id = 2 THEN colors END) AS colors_2,
MAX(CASE WHEN row_id = 2 THEN shapes END) AS shapes_2,
MAX(CASE WHEN row_id = 2 THEN numb END) AS numb_2,
MAX(CASE WHEN row_id = 3 THEN colors END) AS colors_3,
MAX(CASE WHEN row_id = 3 THEN shapes END) AS shapes_3,
MAX(CASE WHEN row_id = 3 THEN numb END) AS numb_3
FROM
sorted
GROUP BY
id,
code,
identifiers