将带分隔符的一行分为多行
今天遇到一个没做过的需求,要把带分隔符的一行多列数据按分隔符分为多行多列插入另个表,同时别的列保持不变,想了一会感觉变化后的数据比较难和变化前的关联起来,网上看了下几个案例,感觉: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导致数据有缺失的问题
本文题目:将带分隔符的一行分为多行
URL分享:http://scpingwu.com/article/jehdjh.html
例如源数据是这样

目标数据是这样

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导致数据有缺失的问题
本文题目:将带分隔符的一行分为多行
URL分享:http://scpingwu.com/article/jehdjh.html