将这种数据 select * from T_B_INFO_TEST
转化成这种数据:select * from T_b_INFO_RESULT
转化数据的脚本:
insert into T_b_INFO_RESULT
select q.c_month,
regexp_substr(q.prod_code, '[^,]+', 1, q.rn) prod_code,
q.c_soto,
q.kpi_value
from (select t.c_month, t.prod_code, t.c_soto, t.kpi_value, t1.rn
from (select a.c_month,
a.prod_code,
a.c_soto,
a.kpi_value,
length(a.prod_code) -
length(replace(a.prod_code, ',')) + 1 rn
from t_B_info_test a) t,
(select level rn
from dual
connect by level <=
(select max(length(b.prod_code) -
length(replace(b.prod_code, ',')) + 1) rn
from t_B_info_test b)) t1
where t.rn >= t1.rn) q;
其中涉及到的知识点:
select level from dual connect by level<=10;
select rownum from dual connect by rownum<=10;
select level from dual connect by rownum<=10;
select rownum from dual connect by level<=10;
这四条语句的用法相同,都是取1到10之间的数字
REGEXP_SUBSTR字符串截取正则表达式
select regexp_substr('A,B,C,D', '[^,]+', 1, ROWNUM)
from dual
CONNECT BY ROWNUM <= 4;
REGEXP_SUBSTR是字符串截取正则表达式:A,B,C,D是要截取的字符串;[^,]+是正则表达式匹配模式,匹配以非逗号的任意字符开始,以非逗号的任意字符结束的一段字符,1是从第一个字符开始,ROWNUM 是匹配第ROWNUM字符串。