i want to get just the end parent for each child using oracle connect by , start with statement
I am using start with , connect by statement to get data recursivly,开发者_JS百科 i am getting all parent - child but i just want to get the end parent for each child. for eg i have following data
child --> parent
a ------> b,
b ------> c,
c ------> d,
c ------> e
so i want the output just
a --> d,
and a --> e
my query is
SELECT LEVEL, cp.child, cp.parent FROM child_parent cp
CONNECT BY nocycle PRIOR cp.parent= cp.child
START WITH cp.child= a
can any body help me with this.
It's unclear whether you want to hard code your root 'a' (which makes the solution a bit easier) or if you want to have a more generic solution for multiple roots. Assuming the former, then this might get you going:
create table child_parent (
parent varchar2(2),
child varchar2(2)
);
insert into child_parent values (null, 'a');
insert into child_parent values ( 'a', 'b');
insert into child_parent values ( 'b', 'c');
insert into child_parent values ( 'c', 'd');
insert into child_parent values ( 'c', 'e');
insert into child_parent values (null, 'k');
insert into child_parent values ( 'k', 'l');
insert into child_parent values ( 'l', 'm');
insert into child_parent values ( 'l', 'n');
with choose_root_here as (
select 'a' as root from dual
)
select
choose_root_here.root || '->' || child from
(
select
level lvl,
connect_by_isleaf leaf,
cp.parent,
cp.child
from
child_parent cp
connect by nocycle prior cp.child= cp.parent
start with
cp.child='a'
)
cross join choose_root_here
where
leaf = 1;
If you want a more generic solution for any root, the this might help
Edit, for parent cannot be null:
select
substr(regexp_replace(path, '##(\w+).*##(\w+)', '\1->\2'),1,30) path
from (
select
level lvl,
substr(sys_connect_by_path(child, '##'),1,20) path,
connect_by_isleaf leaf,
cp.parent,
cp.child
from
child_parent cp
connect by nocycle prior cp.child= cp.parent
start with
cp.parent is null
)
where leaf = 1;
精彩评论