1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| with table1 (id, lie_a) as
(
select 1, '5' from dual union all
select 2, '8, 16' from dual union all
select 3, '7' from dual
)
, table2 (id, nom) as
(
select 5, 'pomme' from dual union all
select 7, 'poire' from dual union all
select 8, 'raisin blanc' from dual union all
select 16, 'raisin noir' from dual
)
, table1_rec (id, lie_a, lie_reste, lvl, nb_recurs) as
(
select id
, substr(lie_a, 1, instr(lie_a, ',') - 1)
, substr(lie_a, instr(lie_a, ',') + 2)
, 1
, length(lie_a) - length(replace(lie_a, ',', ''))
from table1
where instr(lie_a, ',') > 0
union all
select id
, case lvl
when nb_recurs
then substr(lie_reste, 1)
else substr(lie_reste, 1, instr(lie_reste, ',') - 1)
end
, substr(lie_reste, instr(lie_reste, ',') + 2)
, lvl + 1
, nb_recurs
from table1_rec
where lvl <= nb_recurs
)
, table1_finale (id, lie_a, ord) as
(
select id, to_number(lie_a), lvl
from table1_rec
union all
select id, to_number(lie_a), 1
from table1
where instr(lie_a, ',') = 0
)
select t1.id
, listagg(t2.nom, ', ') within group (order by t1.ord asc) as nom
from table1_finale t1
left join table2 t2 on t2.id = t1.lie_a
group by t1.id; |
Partager