本帖最后由 chiyhong 于 2015-10-22 17:19 编辑
如果用 with 迭代语句 或者 connnect by生成一个树后,需要对有一个层级 打断,然后在该层级上添加新的数据 后 在把打断的树 拼接完成..... 如下:
层级 1
1
层级 2
2 , 3
层级 3
4 5 6 , 678
如果想在层级2 把 3的的分支 用以下数据替换
层级 1
3
层级 2
11 ,12
最终想要的结果是:
层级 1
1
层级 2
2
,
3
层级 3
4 5 6 , 11 , 12
层级 4
678,678
需要做层级替换,还要做一个 字层级的笛卡尔积 ,还要维护 层级 ,兄弟们有高招吗?
或者说如何在建树的时候 把 2个表的数据 遍历成上面的那棵树?
create table taba (id number ,name varchar2(255),pid int);
create table tabb (id number ,name varchar2(255),pid int);
insert into taba values(1, 'P1-S1-T1', '');
insert into taba values(2, 'G1', 1);
insert into taba values(3, 'P4-S1-T1', 1);
insert into taba values(4, 'P2-S1-T1', 2);
insert into taba values(5, 'P5-S1-T1', 2);
insert into taba values(6, 'P6-S1-T1', 2);
insert into taba values(7, 'P3-S1-T1',4 );
insert into tabb values(2, 'G1', 1);
insert into tabb values(4, 'P2-S1-T1', 2);
insert into tabb values(11, 'G2',2 );
insert into tabb values(12, 'P6',2 );
insert into tabb values(13, 'P1',4 );
--原始的树
with t(id,
name,
pid,
nlevel) as
(select id, name, pid, 1 nlevel
from taba
where name = 'G1'
union all
select s.id, s.name, s.pid, 1 + t.nlevel
from t, taba s
where t.id = s.pid)
select * from t
--要添加的树
with t(id,
name,
pid,
nlevel) as
(select id, name, pid, 1 nlevel
from tabb
where pid = 2
union all
select s.id, s.name, s.pid, 1 + t.nlevel
from t, tabb s
where t.id = s.pid )
select * from t
|