Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.server -> Re: hierarchical query - returning a treewalked subset
Ah... Now I understand your problem. Avoiding duplicates complicates
matters, because then you will have to traverse the tree the other way
to see if any other candidate is listed higher, starting from all rows
that satisfy your "start with" criteria.
The select below is even slower, but still works for this problem. I suspect there is an easier way to solve this :-)
"Kevin" will result in two rows, one being the boss for the other, so
"Kevin" the lowlife employee will be excluded from the "start with"-
clause.
Havard
with emp_start as (
select emp2.employee_id /* All potential candidates */
from employees emp2
where emp2.first_name = 'Kevin')
select level, emp.*
from employees emp
start with emp.employee_id in (select es.employee_id /* Valid
candidates */
from emp_start es where 1 = (select count(*) /* Only one candidate up the tree*/ from employees emp3 start with emp3.employee_id = es.employee_id connect by prior emp3.manager_id = emp3.employee_id andemp3.employee_id in (select es2.employee_id
from emp_start es2
) ) )
![]() |
![]() |