Рекурсивный запрос
Версия 2.1 от Alexandr Fokin на 2020/07/18 14:02
with recursive tree (nm, id, level, pathstr)
as
(
--Первый элемент в выборке. Начало рекурсии
select
nm, id, 0, cast('' as text)
from tree_sample
where id_parent is null
union all
--Каждый последующий элемент рекурсии
select
tree_sample.nm, tree_sample.id, t.level + 1, tree.pathstr + tree_sample.nm
from tree_sample
inner join tree
on tree.id = tree_sample.id_parent
)
select
id, space( level ) + nm as nm
from tree
order by pathstr