Subject | Re: [firebird-support] Common Table Expressions and Sorting |
---|---|
Author | Stefan Heymann |
Post date | 2009-05-05T13:33:33Z |
with recursive
dept_tree (dept_id, name, lvl, sgn )
as (
select dept_id, name, 0, 0 as lvl
from depts where parent_dept_id is null
union all
select d.dept_id, d.name, h.lvl + 1, 1
from depts d
join dept_tree h on d.parent_dept_id = h.dept_id
)
select z.dept_id, z.sgn, z.lvl, z.name
from dept_tree z
order by z.sgn, upper(z.name)
Thanks, Mimmo. But in your version (I had to correct it to the above)
the nesting gets lost. All levels below root level have a Sign value
of 1 (one). Is that how you intended it to be?
Regards
Stefan
dept_tree (dept_id, name, lvl, sgn )
as (
select dept_id, name, 0, 0 as lvl
from depts where parent_dept_id is null
union all
select d.dept_id, d.name, h.lvl + 1, 1
from depts d
join dept_tree h on d.parent_dept_id = h.dept_id
)
select z.dept_id, z.sgn, z.lvl, z.name
from dept_tree z
order by z.sgn, upper(z.name)
Thanks, Mimmo. But in your version (I had to correct it to the above)
the nesting gets lost. All levels below root level have a Sign value
of 1 (one). Is that how you intended it to be?
Regards
Stefan