Zapytanie hierarchiczne

Przykłady ze szkolenia SQL Oracle na Udemy:
Ustawienie prostej hierarchii (kolumna level oznacza ilość przelożonych)

select
employee_id,
last_name,
manager_id,
level
from employees
connect by prior employee_id = manager_id


Podwładni konkretnego pracownika

select
employee_id,
last_name,
manager_id,
level
from employees
START WITH employee_id = 101
connect by prior employee_id = manager_id


Wypisanie w dodatkowej kolumnie pełnej hierarchii z separatorem '/'

select
employee_id,
last_name,
manager_id,
level,
SYS_CONNECT_BY_PATH(last_name, '/') pather,
CONNECT_BY_ISCYCLE
from employees
START WITH employee_id = 100
connect by NOCYCLE prior employee_id = manager_id


Wypisanie samej hierarchi w formie kaskadowej

select
LPAD(' ', 2*level) ||
SYS_CONNECT_BY_PATH(last_name, '/') pather
from employees
START WITH employee_id = 100
connect by prior employee_id = manager_id

Tags: