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. :)