2017-03-22 3 views
1

私は、次の表を持っている:stringから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  d value cost 
150 itema section shelf 5 0.5 
120 itemb shelf  box  5 2.3 
151 itemf section shelf 5 1.5 

は、基本的には、4つの異なる列にtlb列を破ります。

tlbの構造は常に同じstring;string;string;string

私はそれをどのように行うことができますか?

+1

あなたは 'split_part'などを使用することができます –

答えて

1

あなたがsplit_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) 
1

はこれを示す:

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 
関連する問題