我有一张桌子,里面有州,地区,区,建筑和类的树。每行都有一个节点ID和一个父ID。我使用以下
select name, child node, parent id from tableA
connect by prior child node = parent id
我得到:
CA
CENTRAL REGION
FRESNO DISTRICT
ST Jim BUILDING
ST joe BUILDING
st tom BUILDING
st sue BUILDING
JIMS CLASS
JOES CLASS
TOM CLASS
SUE CLASS
问题是,在建筑层面,所有建筑物都是一个接一个地列出,然后是所有类别。但是我想列出建筑物和类,它是下一个建筑物的父母,如下所示
FRESNO DISTRICT
st jim building
jims class
st joes building
joes class...
节点ID和父ID是正确的,只是列出了父节点的所有子节点,然后是该子节点的所有子节点。它没有显示哪个孩子和父母一起去。
我想从上到下显示树,例如CA,中部地区,弗雷斯诺区,St Joe Building,Mary女士班,女士
答案 0 :(得分:3)
除非您使用不带SIBLINGS关键字的ORDER BY,否则应保留层次结构排序。
select name, id, parent_id
from tableA
connect by prior id = parent_id
start with parent_id is null
order siblings by name;
使用此数据:
create table tableA(id number, name varchar2(100), parent_id number);
insert into tableA
select 1 id, 'CA' name, null parent_id from dual union all
select 2, 'CENTRAL REGION', 1 from dual union all
select 3, 'FRESNO DISTRICT', 2 from dual union all
select 4, 'ST Jim BUILDING', 3 from dual union all
select 5, 'ST joe BUILDING', 3 from dual union all
select 6, 'st tom BUILDING', 3 from dual union all
select 7, 'st sue BUILDING', 3 from dual union all
select 8, 'JIMS CLASS', 4 from dual union all
select 9, 'JOES CLASS', 5 from dual union all
select 10, 'TOM CLASS', 6 from dual union all
select 11, 'SUE CLASS', 7 from dual;