SQL 服务器 - 将行转换为列(带有附加行数据)
SQL Server - Pivot Convert rows to columns (with additional row data)
有人可以帮我解决这个问题 SQL 吗?
我希望通过以下方式将多行转换为列:id、类型、颜色、日期
挑战:我有多个列想要 preserve/be 显示在旋转的 table 上。
id | type | color | date | country_code | cost
---+--------+--------+-----------+--------------+-------
1 | report | red | 2020-09-01| US | 1
1 | report | red | 2020-09-01| EU | 2
1 | report | red | 2020-09-01| RU | 3
1 | report | red | 2020-09-01| AP | 4
1 | report | blue | 2020-09-02| US | 5
1 | report | blue | 2020-09-02| EU | 6
1 | report | blue | 2020-09-02| RU | 7
1 | report | blue | 2020-09-02| AP | 8
2 | report | green | 2020-09-02| US | 9
2 | report | green | 2020-09-02| EU | 10
2 | report | green | 2020-09-02| RU | 11
2 | report | green | 2020-09-02| AP | 12
2 | report | blue | 2020-09-03| US | 13
2 | report | blue | 2020-09-03| EU | 14
2 | report | blue | 2020-09-03| RU | 15
2 | report | blue | 2020-09-03| AP | 16
期望的输出:
id | type | color | date | US | EU | RU | AP
---+--------+-------+------------+----+----+----+----
1 | report | red | 2020-09-01 | 1 | 2 | 3 | 4
1 | report | blue | 2020-09-02 | 5 | 6 | 7 | 8
2 | report | green | 2020-09-02 | 9 | 10 | 11 | 12
2 | report | blue | 2020-09-03 | 13 | 14 | 15 | 16
已知信息:
- 只有4个国家代码。
color
值将相同:id、类型、日期。
不确定什么是最 cleanest/best 的写法 SQL。
我试过
ROW_NUMBER() OVER(PARTITION BY xxx ORDER BY yyy)
和PIVOT
但无法得到我想要的结果
我认为条件聚合可以满足您的需求:
select id, type, color, date,
max(case when country_code = 'US' then cost end) as us,
max(case when country_code = 'EU' then cost end) as eu,
max(case when country_code = 'RU' then cost end) as ru,
max(case when country_code = 'AP' then cost end) as AP
from t
group by id, type, color, date;
我玩了一下,觉得我明白了:
SELECT id, type, color, date, [US], [EU], [RU], [AP]
FROM
(
SELECT id, type, color, date, country_code, cost FROM tableX
) t
PIVOT
(
MAX(cost)
FOR country_code IN ( [US], [EU], [RU], [AP] )
) p
有人可以帮我解决这个问题 SQL 吗?
我希望通过以下方式将多行转换为列:id、类型、颜色、日期
挑战:我有多个列想要 preserve/be 显示在旋转的 table 上。
id | type | color | date | country_code | cost
---+--------+--------+-----------+--------------+-------
1 | report | red | 2020-09-01| US | 1
1 | report | red | 2020-09-01| EU | 2
1 | report | red | 2020-09-01| RU | 3
1 | report | red | 2020-09-01| AP | 4
1 | report | blue | 2020-09-02| US | 5
1 | report | blue | 2020-09-02| EU | 6
1 | report | blue | 2020-09-02| RU | 7
1 | report | blue | 2020-09-02| AP | 8
2 | report | green | 2020-09-02| US | 9
2 | report | green | 2020-09-02| EU | 10
2 | report | green | 2020-09-02| RU | 11
2 | report | green | 2020-09-02| AP | 12
2 | report | blue | 2020-09-03| US | 13
2 | report | blue | 2020-09-03| EU | 14
2 | report | blue | 2020-09-03| RU | 15
2 | report | blue | 2020-09-03| AP | 16
期望的输出:
id | type | color | date | US | EU | RU | AP
---+--------+-------+------------+----+----+----+----
1 | report | red | 2020-09-01 | 1 | 2 | 3 | 4
1 | report | blue | 2020-09-02 | 5 | 6 | 7 | 8
2 | report | green | 2020-09-02 | 9 | 10 | 11 | 12
2 | report | blue | 2020-09-03 | 13 | 14 | 15 | 16
已知信息:
- 只有4个国家代码。
color
值将相同:id、类型、日期。
不确定什么是最 cleanest/best 的写法 SQL。
我试过
ROW_NUMBER() OVER(PARTITION BY xxx ORDER BY yyy)
和PIVOT
但无法得到我想要的结果
我认为条件聚合可以满足您的需求:
select id, type, color, date,
max(case when country_code = 'US' then cost end) as us,
max(case when country_code = 'EU' then cost end) as eu,
max(case when country_code = 'RU' then cost end) as ru,
max(case when country_code = 'AP' then cost end) as AP
from t
group by id, type, color, date;
我玩了一下,觉得我明白了:
SELECT id, type, color, date, [US], [EU], [RU], [AP]
FROM
(
SELECT id, type, color, date, country_code, cost FROM tableX
) t
PIVOT
(
MAX(cost)
FOR country_code IN ( [US], [EU], [RU], [AP] )
) p