“Connect By” to generate rows from multiple delimi

2019-08-09 15:35发布

问题:

we can use "Connect By" to generate rows from a delimited string in oracle. like:

SELECT Rn ,Regexp_Substr(data, '[^,]+', 1, LEVEL) Data
FROM (SELECT 1 Rn ,'id:a,val:b,desc:c' data FROM Dual) Idata
CONNECT BY Regexp_Substr(data, '[^,]+', 1, LEVEL) IS NOT NULL

I want to use the inner query as a union all of a few more records. Something like:

SELECT Rn ,Regexp_Substr(data, '[^,]+', 1, LEVEL) Data
    FROM (SELECT 1 Rn ,'id:a,val:b,desc:c' data FROM Dual
      UNION ALL
      SELECT 2 Rn ,'id:a2,val:b2,desc:c2' data FROM Dual
      UNION ALL
      SELECT 3 Rn ,'id:a3,val:b3,desc:c3' data FROM Dual) Idata
    CONNECT BY Regexp_Substr(data, '[^,]+', 1, LEVEL) IS NOT NULL;

So that I could get a result set as,

RN  DATA
1   desc:c
1   id:a
1   val:b
2   desc:c2
2   id:a2
2   val:b2
3   desc:c3
3   id:a3
3   val:b3

But it is not working properly, it is coming as :

RN  DATA
1   desc:c
1   desc:c
1   desc:c
1   desc:c
1   desc:c
1   desc:c
1   desc:c
1   desc:c
1   desc:c
1   id:a
1   val:b
1   val:b
1   val:b
2   desc:c2
2   desc:c2
2   desc:c2
2   desc:c2
2   desc:c2
2   desc:c2
2   desc:c2
2   desc:c2
:
:
:

Applying DISTINCT is not the target. because the strings could be different and here it is taking huge time to split for bigger strings. Something LEVEL generation is not proper, I guess, in this query. May be, group by facility over Rn may need to be used. Can any body help me out to write this query? Thanks n advance. :)

回答1:

If you're using 11gR2 you can use RCTE:

with Idata as
(SELECT 1 Rn ,'id:a,val:b,desc:c' data FROM Dual
      UNION ALL
      SELECT 2 Rn ,'id:a2,val:b2,desc:c2' data FROM Dual
      UNION ALL
      SELECT 3 Rn ,'id:a3,val:b3,desc:c3' data FROM Dual),
rcte(rn, txt, token, i) as
(
  select rn, data, Regexp_Substr(data, '[^,]+', 1, 1), 2
  from Idata
  union all
  select rn, txt, Regexp_Substr(txt, '[^,]+', 1, i), i+1
  from rcte 
  where Regexp_Substr(txt, '[^,]+', 1, i) IS NOT null
  )
select rn, token
from rcte
order by rn;

If not, and adding DISTINCT is to heavy, then you can try a different approach such as using a pipelined function-

create or replace type t is object(token varchar2(100));
/
create or replace type t_tab as table of t;
/
create or replace function split_string(str varchar2, del in varchar2) return t_tab
  pipelined is

  token    varchar2(4000);
  str_t   varchar2(4000) ;
  v_del_i number;


begin

    str_t := str;

    while str_t is not null loop

      v_del_i := instr(str_t, del, 1, 1);

      if v_del_i = 0 then
        token  := str_t;
        str_t := '';
      else
        token  := substr(str_t, 1, v_del_i - 1);
        str_t := substr(str_t, v_del_i + 1);
      end if;

      pipe row(t(token));

    end loop;

 return;
end split_string;
/

Now the query could look like this:

select t.token, Idata.rn
from (SELECT 1 Rn ,'id:a,val:b,desc:c' data FROM Dual
      UNION ALL
      SELECT 2 Rn ,'id:a2,val:b2,desc:c2' data FROM Dual
      UNION ALL
      SELECT 3 Rn ,'id:a3,val:b3,desc:c3' data FROM Dual) Idata ,
      table(split_string(Idata.data, ',')) t 

Here is a sqlfiddle demo