關於oracle 拆分字串加列轉行的問題

2021-06-19 21:23:51 字數 3166 閱讀 5455

我有這樣一張表vvv 類似
jpczzh_id                jpczzh_ch_ndjsnrjgm

jpczzh0000000004[,]

我想查詢得到這樣的結果

jpczzh_id

jpczzhjsyear1

jpczzhchndjsnrjgm1

jpczzhjsyear2

jpczzhchndjsnrjgm2

1jpczzh0000000004

'2013'

'22'

'121'

'22'

也就是說把jpczzh_ch_ndjsnrjgm

裡的內容都拆成字段。冒號前面的是欄位名(欄位名要依次加123的),後面是值,大括號括的有n組內容,就拆出來n乘以3個字段來。

下面是我的解決辦法
先建乙個檢視
create or replace view v_szh$ndjsnrjgm as

select rn,

jpczzh_id,

p1,substr(cv0, 0, instr(cv0, ':', 1, 1) - 1) || p1 a,

substr(cv0, instr(cv0, ':', 1, 1) + 1) b

from (select t.rn,t.jpczzh_id,t.p1,

substr(t.ca,

instr(t.ca, ',', 1, c.lv) + 1,

instr(t.ca, ',', 1, c.lv + 1) - (instr(t.ca, ',', 1, c.lv) + 1)) as cv0

from (select result1.rn rn,p1,jpczzh_id,',' || cv1 || ',' as ca,

length(cv1 || ',') - nvl(length(regexp_replace(cv1, ',', '')), 0) as cnt

from (select t1.rn,t1.jpczzh_id,c1.lv as p1,

substr(t1.ca,

instr(t1.ca, '},', 1, c1.lv) + 2,

instr(t1.ca, '},', 1, c1.lv + 1) -

(instr(t1.ca, '},', 1, c1.lv) + 2)) as cv1

from (select rownum rn,jpczzh_id,

'},' || regexp_replace(jpczzh_ch_ndjsnrjgm ,'\[\]','') || '},' as ca,

(length(regexp_replace(jpczzh_ch_ndjsnrjgm,'\[\]','') || '},') -

nvl(length(regexp_replace(jpczzh_ch_ndjsnrjgm,'\[,|}\]','')),0)) / 2 as cnt

from jnjp_project_czzh) t1,

(select level lv from dual connect by level <= 10) c1

where c1.lv <= t1.cnt) result1) t,

(select level lv from dual connect by level <= 10) c

where c.lv <= t.cnt

order by jpczzh_id, p1, lv);

出來的結果是這樣的:
rnjpczzh_id               p1 a                            b

11jpczzh00000000041jpczzhjsyear1 '2013'

21jpczzh00000000041jpczzhchndjsnrjgm1 '22'

31jpczzh00000000042jpczzhjsyear2 '121'

41jpczzh00000000042jpczzhchndjsnrjgm2 '22'

然後再查詢:
select *  

from vvv t

left join (select jpczzh_id id,

max(decode(a, 'jpczzhjsyear1', b)) jpczzhjsyear1,

max(decode(a, 'jpczzhchndjsnrjgm1', b)) jpczzhchndjsnrjgm1,

max(decode(a, 'jpczzhjsyear2', b)) jpczzhjsyear2,

max(decode(a, 'jpczzhchndjsnrjgm2', b)) jpczzhchndjsnrjgm2,

max(decode(a, 'jpczzhjsyear3', b)) jpczzhjsyear3,

max(decode(a, 'jpczzhchndjsnrjgm3', b)) jpczzhchndjsnrjgm3,

max(decode(a, 'jpczzhjsyear4', b)) jpczzhjsyear4,

max(decode(a, 'jpczzhchndjsnrjgm4', b)) jpczzhchndjsnrjgm4,

max(decode(a, 'jpczzhjsyear5', b)) jpczzhjsyear5,

max(decode(a, 'jpczzhchndjsnrjgm5', b)) jpczzhchndjsnrjgm5

from v_szh$ndjsnrjgm

group by jpczzh_id) ndjsnrjgm

on ndjsnrjgm.id = t.jpczzh_id

得出結果:

jpczzh_id

jpczzhjsyear1

jpczzhchndjsnrjgm1

jpczzhjsyear2

jpczzhchndjsnrjgm2

1jpczzh0000000004

'2013'

'22'

'121'

'22'

不知道還有沒有好的解決辦法,如果有的話請大家指教

Oracle 拆分字串

create or replace function splitstr p string in varchar2,p delimiter in varchar2 return str split pipelined as v length number length p string v start...

oracle拆分字串

create or replace type array string is table of varchar2 2000 create or replace function f split string 拆分字串 author zhaohuihua i src in varchar2,待拆分的字...

oracle拆分字串

procedure hand mid sys bpm use role iorgtype in bpm compsite user.orgtype type,idate in date is v orgtype bpm compsite user.orgtype type iorgtype v id...