gpt4 book ai didi

sql - 将行转置为列并连接表

转载 作者:行者123 更新时间:2023-12-02 05:30:45 27 4
gpt4 key购买 nike

嗨,我有两个表属性,实例

属性表

id  |    key_info   |   value_info
2 | amount | 1009
2 | currency | USD
2 | Level | Level 5
3 | amount | 2017
3 | currency | CAD

实例表
id   |  status
2 | Pending
3 | Approved

我想像这样连接两个表-

新表
id | amount |  currrency | level   | status
2 | 1001 | USD | Level 5 | Pending
3 | 2017 | CAD | | Approved

Attribute 和 Instance 中的所有字段都是可选的,除了 id。

最佳答案

select
a.id,
max(case when a.key_info = 'amount' then a.value_info end) as amount,
max(case when a.key_info = 'currency' then a.value_info end) as currency,
max(case when a.key_info = 'level' then a.value_info end) as level,
i.status
from
attribute a
join instance i on a.id = i.id
group by
a.id,
i.status

Sql Fiddle

关于sql - 将行转置为列并连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15234212/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com