今天工作被一个递归嵌套卡住了,废话不多说,代码如下:
select t.*
from (
select cporg.code as lid,
cporg.name as level_name,
cporg.SRD_CODE as parent_id,
cporg.type_flag as type_flag
from (
with recursive cte as(
select x.* from gf_sys_organization x
where x.code = '10520301'
union all
select y.* from gf_sys_organization y
inner join cte c on c.code = y.srd_code
and y.catagory = '1'
)select code,name,SRD_CODE,1 as type_flag, 'cadre1' icon_cls from cte
union all
select cp.id, cp.level_name, cp.ORG_CODE, -1 as type_flag, 'cadre2' icon_cls
from GF_cadre_position cp
) cporg
start with cporg.code = '10520301'
connect by prior cporg.code = cporg.SRD_CODE
) t order by t.level_falg, t.type_flag asc
可以清楚地看到是先递归了一次查出结果集,然后再在这个结果集中再递归查询,进行层次标记。
按照正常的思路应该有如下代码: