如何在postgresql中将字符串中的值分隔为列?
我有下表:如何在postgresql中将字符串中的值分隔为列?,sql,postgresql,Sql,Postgresql,我有下表: tlb (Text) value(Integer) cost (numeric) 150;itema;section;shelf 5 0.5 120;itemb;shelf;box 5 2.3 151;itemf;section;shelf 5 1.5 我要将此表转换为此表: a b c
tlb (Text) value(Integer) cost (numeric)
150;itema;section;shelf 5 0.5
120;itemb;shelf;box 5 2.3
151;itemf;section;shelf 5 1.5
我要将此表转换为此表:
a b c d value cost
150 itema section shelf 5 0.5
120 itemb shelf box 5 2.3
151 itemf section shelf 5 1.5
基本上将tlb
列拆分为4个不同的列
tlb
的结构总是相同的字符串;一串一串字符串
我如何才能做到这一点?展示以下内容:
select
exe2.arr[1] as a,
exe2.arr[2] as b,
exe2.arr[3] as c,
exe2.arr[4] as d
from
(
select
exe.x,
string_to_array(exe.x,';') as arr
from
(
select
'150;itema;section;shelf' as x
union select
'120;itemb;shelf;box' as x
union select
'151;itemf;section;shelf' as x
) exe
) exe2
您可以使用
split\u part
,例如:
t=# with s as (select '150;itema;section;shelf'::text d)
select
split_part(d,';',1) a
, split_part(d,';',2) b
, split_part(d,';',3) c
, split_part(d,';',4) d
from s;
a | b | c | d
-----+-------+---------+-------
150 | itema | section | shelf
(1 row)
您可以使用
split\u part
eg