有一张桌子:PS_POSITION_TBL
/
我们想在上面构建层次结构的想法
案例1:038,0418和114向143报告
案例2:346和202向114报告
所以最高级别是level1,这是143,然后038,0418,114是级别2,因为他们报告到级别1然后346和202是级别3并且它们属于 3级桶,他们报告到2级
这些可以达到8级(MAX)
我需要的是在Oracle中以下列格式获取数据的查询:
position_nbr reports_to descr
038 143 DIRECTOR
0418 143 ADVISOR
114 143 DG
346 114 Manager
202 114 Lead
答案 0 :(得分:1)
以下查询将为您提供一个分层集,由LEVEL
左侧填充:
SELECT LPAD(' ', level * 2, ' ') || TO_CHAR(position_nbr), descr
FROM PS_POSITION_TBL
CONNECT BY PRIOR position_nbr = reports_to
START WITH reports_to IS NULL;
如果您对在单个列中显示级别(LEVEL
)感到不满意,并且每个级别都需要一个列,则可以基于LEVEL
使用PIVOT但它会使订购有问题。
答案 1 :(得分:1)
select level1,level2,level3,level4,level5,level6,level7,level8,descr
from (select level as n,position_nbr as id,descr,position_nbr
from ( select position_nbr ,reports_to ,descr from PS_POSITION_TBL
union all select 143 ,null ,'CEO' from dual
) t
start with reports_to is null
connect by reports_to = prior position_nbr
)
pivot (max(position_nbr) for n in (1 as level1,2 as level2,3 as level3,4 as level4,5 as level5,6 as level6,7 as level7,8 as level8))
;
或
select decode (n,1,position_nbr) as level1
,decode (n,2,position_nbr) as level2
,decode (n,3,position_nbr) as level3
,decode (n,4,position_nbr) as level4
,decode (n,5,position_nbr) as level5
,decode (n,6,position_nbr) as level6
,decode (n,7,position_nbr) as level7
,decode (n,8,position_nbr) as level8
,descr
from (select level as n,position_nbr,descr
from ( select position_nbr ,reports_to ,descr from PS_POSITION_TBL
union all select 143 ,null ,'CEO' from dual
) t
start with reports_to is null
connect by reports_to = prior position_nbr
)
;