17,378
社区成员
发帖
与我相关
我的任务
分享with
t1 as (select ID, PARENTID,TYPE from
(select ID, PARENTID,TYPE,row_number()over(order by level)rn from pm_test where type=0 start with ID in (400)
connect by prior PARENTID=ID)
where rn=1
),
t2 as (select ID, PARENTID,TYPE from
(select ID, PARENTID,TYPE,row_number()over(order by level)rn from pm_test where type=0 start with ID in (300)
connect by prior PARENTID=ID)
where rn=1
)
select * from t1
union all
select * from t2;
create table pm_test(ID number, PARENTID number ,TYPE number)
insert into pm_test values(1, NULL, 0);
insert into pm_test values(2, NULL, 0);
insert into pm_test values(100, 1, null);
insert into pm_test values(200, 2, 0);
insert into pm_test values(300, 100, null);
insert into pm_test values(400, 200, null);
commit;
select * from(
select t.*,level,row_number() over(order by level) rn from pm_test t where type=0 connect by prior parentid=id start with id=400)
where rn=1
-----------------
rownum ID PARENTID TYPE LEVEL RN
1 200 2 0 2 1