Hierarchy SQL Problem Script-1
Hierarchy SQL Problem Script-1
-- SOLUTION
with recursive cte as
(select emp_id, emp_id as Employees_working_under_him--, 1 as iteration
from Employee_Hierarchy --where Reporting_Id is null
union all
select cte.emp_id, h.emp_id as Employees_working_under_him--, (iteration+1)
as iteration
from cte
join Employee_Hierarchy h on cte.Employees_working_under_him =
h.Reporting_Id
)
select *
from cte
order by 1,2
-- BREAKUP
select emp_id, emp_id as Employees_working_under_him, 1 as iteration
from Employee_Hierarchy where Reporting_Id is null