Oracle查询直观展示树形结构数据

针对树形结构数据为了展现其直观层级结构的探究

数据样式及展示结果如图:

 

 结果图:

 

解析:

1.使用临时表自关联添加级别,临时表数据如下查询:

select t.trid, t.trname, t.prtrid, level le
from t_test_tree t
start with t.prtrid=0
connect by prior t.trid = t.prtrid

2.根据级别层级关联设置字段展现数据:

WITH
temp1 AS (select t.trid, t.trname, t.prtrid, level le
from t_test_tree t
start with t.prtrid=0
connect by prior t.trid = t.prtrid)
select a.prtrid trid0,a.trid trid1,a.trname trname1,b.trid trid2,b.trname trname2
       ,c.trid trid3,c.trname trname3,d.trid trid4,d.trname trname4,nvl(nvl2(d.trid,d.trid,c.trid),b.trid) trid,
        nvl(nvl2(d.trname,d.trname,c.trname),b.trname) trname
 from temp1 a,temp1 b,temp1 c,temp1 d
where 1=1
and a.trid=b.prtrid(+)
and b.trid=c.prtrid(+)
and c.trid=d.prtrid(+)
and a.le(+)=1
and b.le(+)=2
and c.le(+)=3
and d.le(+)=4
order by a.trid,b.trid,c.trid,d.trid
;

posted @ 2020-04-20 15:14  海月孤影  阅读(717)  评论(0编辑  收藏  举报