Saturday, February 25, 2012

Possible newbie SQL question. Help.

I have the below table

Organization:
emp_id, emp_name, manager_id, level

level 3 employees report to level 2 employees than report to level 1 employees

I need to select all level 3 employees, their direct manager (level 2) and indirect manager (level1).

Any help?Treat each level as a separate table:

select l3.emp_name, l2.emp_name, l1.emp_name
from emp l1, emp l2, emp l3
where l1.emp_id = l2.manager_id
and l2.emp_id = l3.manager_id;

Or in Oracle there is "CONNECT BY" to perform a tree-structured query.

No comments:

Post a Comment