sql - Oracle - column to all combination rows -


i have oracle table varchar(100) column, , need separate rows based on strings combinations inside column. string delimiter ' ' (space) , number of strings variable.

this example:

select 1 id, 'string_1 string_2 string_3 string_n' name dual 

the output need:

id  name --  ------ 1, 'string_1 string_2 string_3 string_n' 1, 'string_1 string_2 string_3' 1, 'string_1 string_2' 1, 'string_1' 1, 'string_2 string_3 string_n' 1, 'string_2 string_3' 1, 'string_2' 1, 'string_3 string_n' 1, 'string_3' 1, 'string_n' 

*this minimum output want, can handle possible combinations.

sql> create table mytable (id,name)   2    3  select 1, 'string_1 string_2 string_3 string_n' dual union   4  select 2, null dual union   5  select 3, 'ma lo' dual   6  /  table created.  sql> t   2  ( select id   3         , name   4         ,   5      mytable   6     model   7           return updated rows   8           partition (id)   9           dimension (0 i)  10           measures (name)  11           ( name[for 1 regexp_count(name[0],' ')+1 increment 1]  12             = regexp_substr(name[0],'[^ ]+',1,cv(i))  13           )  14  )  15  , t2 (id,name,i)  16  ( select id  17         , name  18         ,  19      t  20     union  21    select t.id  22         , t.name || ' ' || t2.name  23         , t.i  24      t  25         , t2  26     t.id = t2.id  27       , t.i < t2.i  28  )  29  select id  30       , name  31    t2  32   order id  33       ,  34       , length(name) desc  35  /          id name ---------- --------------------------------------------------          1 string_1 string_2 string_3 string_n          1 string_1 string_2 string_n          1 string_1 string_3 string_n          1 string_1 string_2 string_3          1 string_1 string_3          1 string_1 string_n          1 string_1 string_2          1 string_1          1 string_2 string_3 string_n          1 string_2 string_3          1 string_2 string_n          1 string_2          1 string_3 string_n          1 string_3          1 string_n          3 ma lo          3 ma          3 lo  18 rows selected. 

requires 11.2 because of recursive subquery factoring used.

regards,
rob.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -