【发布时间】:2021-07-20 00:30:20
【问题描述】:
我最近开始使用 SQL 进行数据操作。所以,如果这太基本了,请原谅我。 我有以下格式的数据表1。
id pg_a pg_b
12 1 0
35 1 1
46 0 1
我想将其转换为如下表所示的长格式。
id pg value
12 a 1
12 b 0
35 a 1
35 b 1
46 a 0
46 a 1
我有一个使用 case when 的 sql 查询,但是运气不好。查询仅在两个 case 语句中执行第一个。
这是查询:
select id,
(case when pg_a is not null then pg_a
when pg_b is not null then pg_b
else null
end) AS pg,
(case when pg_a is not null then a
when pg_b is not null then b
else null
end) AS value
from table1
我需要做些什么不同的事情?任何指针将不胜感激。
提前谢谢你。
【问题讨论】:
标签: sql postgresql data-manipulation