今天遇到一个没做过的需求,要把带分隔符的一行多列数据按分隔符分为多行多列插入另个表,同时别的列保持不变,想了一会感觉变化后的数据比较难和变化前的关联起来,网上看了下几个案例,感觉:https://blog.csdn.net/mchdba/article/details/51946573 的方法比较不错,
例如源数据是这样
目标数据是这样
insert语句为:
insert into temp_0428_test2(pro_key,nip)
with temp0 as
(select LEVEL lv from dual CONNECT BY LEVEL <= 100)
select pro_key, substr(t.vals, instr(t.vals, ',', 1, tv.lv) + 1, instr(t.vals, ',', 1, tv.lv + 1) - (instr(t.vals, ',', 1, tv.lv) + 1)) AS name
from (select pro_key,
',' || nip || ',' AS vals,
length(nip || ',') - nvl(length(REPLACE(nip, ',')), 0) AS cnt
from temp_0428_test1) t
join temp0 tv
on tv.lv <= t.cnt
order by 1;
这个方法不用和rownum做关联,避免了rownum增长后不能重置为1导致数据有缺失的问题
例如源数据是这样
目标数据是这样
insert语句为:
insert into temp_0428_test2(pro_key,nip)
with temp0 as
(select LEVEL lv from dual CONNECT BY LEVEL <= 100)
select pro_key, substr(t.vals, instr(t.vals, ',', 1, tv.lv) + 1, instr(t.vals, ',', 1, tv.lv + 1) - (instr(t.vals, ',', 1, tv.lv) + 1)) AS name
from (select pro_key,
',' || nip || ',' AS vals,
length(nip || ',') - nvl(length(REPLACE(nip, ',')), 0) AS cnt
from temp_0428_test1) t
join temp0 tv
on tv.lv <= t.cnt
order by 1;
这个方法不用和rownum做关联,避免了rownum增长后不能重置为1导致数据有缺失的问题
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29420818/viewspace-2153523/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29420818/viewspace-2153523/