Subject Re: [firebird-support] Common Table Expressions and Sorting
Author masotti
Hi Stefan,

Stefan Heymann ha scritto:
> with recursive
> dept_tree as (
> select dept_id, name, 0 as lvl
> from depts where parent_dept_id is null
>
> union all
>
> select d.dept_id, d.name, h.lvl + 1
> from depts d
> join dept_tree h on d.parent_dept_id = h.dept_id
> -- *1*
> )
> select dept_id, lvl, name
> from dept_tree
>

with recursive
dept_tree (dept_id, lvl, name, sign )
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
-- *1*
)
select z.dept_id, z.lvl, z.name
from dept_tree z
order by z.sign, upper(z.name)

Ciao.
Mimmo.