Vous devez d'abord remonter l'arborescence pour obtenir tous les responsables, puis descendre pour récupérer tous les employés :
select level, employee_id, last_name, manager_id ,
connect_by_root employee_id as root_id
from employees
connect by prior employee_id = manager_id -- down the tree
start with manager_id in ( -- list up the tree
select manager_id
from employees
connect by employee_id = prior manager_id -- up the tree
start with employee_id = 101
)
;
Voir http://www.sqlfiddle.com/#!4/d15e7/18
Modifier :
Si le nœud donné peut également être le nœud racine, étendez la requête pour inclure le nœud donné dans la liste des nœuds parents :
Exemple pour un nœud non racine :
select distinct employee_id, last_name, manager_id
from employees
connect by prior employee_id = manager_id -- down the tree
start with manager_id in ( -- list up the tree
select manager_id
from employees
connect by employee_id = prior manager_id -- up the tree
start with employee_id = 101
union
select manager_id -- in case we are the root node
from employees
where manager_id = 101
)
;
Exemple pour le nœud racine :
select distinct employee_id, last_name, manager_id
from employees
connect by prior employee_id = manager_id -- down the tree
start with manager_id in ( -- list up the tree
select manager_id
from employees
connect by employee_id = prior manager_id -- up the tree
start with employee_id = 100
union
select manager_id -- in case we are the root node
from employees
where manager_id = 100
)
;
Violon à http://www.sqlfiddle.com/#!4/d15e7/32