我有一张这样的表:
gid | cat1 | cat2 | some_value
-------------------------------
1 | A | 100 | I
2 | B | 200 | II
3 | C | 300 | III
4 | D | 400 | IV
我想将此表转换为类似的内容,即将列值转换为行值:
gid | category | value | some_value
-----------------------------------
1 | cat1 | A | I
1 | cat2 | 100 | I
2 | cat1 | B | II
2 | cat2 | 200 | II
... | ... | ... | ...
这是否可以在Postgresql中使用?
答案 0 :(得分:2)
使用Union
:
Select gid, 'cat1' as category, cat1 as value, some_value from t
union all
Select gid, 'cat2' as category, cast(cat2 as varchar(100)) as value, some_value from t
order by gid
这是一个fiddle。