如果填充在第一个参数中完成,您可以使用listagg:
with rcte (id, value, lvl, result) as (
select id, value, 1, regexp_substr(value, '(.*?)( - |$)', 1, 1, null, 1)
from your_table
union all
select id, value, lvl + 1, regexp_substr(value, '(.*?)( - |$)', 1, lvl + 1, null, 1)
from rcte
where regexp_substr(value, '(.*?)( - |$)', 1, lvl + 1, null, 1) is not null
)
select id,
listagg(case when lvl > 1 then rpad(chr(10), lvl, '>') || ' ' end || result)
within group (order by lvl) as result
from rcte
group by id
order by id;
ID | RESULT
-: | :--------------------------------------------------------------------------------
1 | This is the base level
> This is level 2
>> And this is the second
2 | Base only
但你也说过:
我可以开始并用换行符替换“ - ”并应用 CSS pre 以确保它们都在一行上
所以如果你想要一行,你就不能一开始就添加换行符:
with rcte (id, value, lvl, result) as (
select id, value, 1, regexp_substr(value, '(.*?)( - |$)', 1, 1, null, 1)
from your_table
union all
select id, value, lvl + 1, regexp_substr(value, '(.*?)( - |$)', 1, lvl + 1, null, 1)
from rcte
where regexp_substr(value, '(.*?)( - |$)', 1, lvl + 1, null, 1) is not null
)
select id,
listagg(case when lvl > 1 then rpad(' ', lvl, '>') || ' ' end || result)
within group (order by lvl) as result
from rcte
group by id
order by id;
ID | RESULT
-: | :-------------------------------------------------------------------------------
1 | This is the base level > This is level 2 >> And this is the second
2 | Base only
您还可以在递归 CTE 中添加 >,这可能会更整洁:
with rcte (id, value, lvl, result) as (
select id, value, 1, regexp_substr(value, '(.*?)( - |$)', 1, 1, null, 1)
from your_table
union all
select id, value, lvl + 1,
rpad('>', lvl, '>') || ' ' || regexp_substr(value, '(.*?)( - |$)', 1, lvl + 1, null, 1)
from rcte
where regexp_substr(value, '(.*?)( - |$)', 1, lvl + 1, null, 1) is not null
)
select id,
listagg(result, ' ') within group (order by lvl) as result
from rcte
group by id
order by id;
db<>fiddle