SQL query to get the list of supervisor hierarchy. employee --> supervisor --> supervisor

后端 未结 2 1794
广开言路
广开言路 2021-01-23 14:14

I have two tables Employee and Department this image shows the manager of every employee. I want to write a SQL query that gives me a list of all the supervisor (Mana

2条回答
  •  别那么骄傲
    2021-01-23 14:55

    This is the solution to my question

      with recursive p as (
    WITH emp_dept as(
    SELECT employee_id,manager_id 
    FROM employee,department
    WHERE employee.dept_id= department.dept_id
    ) 
    
    select e1.employee_id, e1.manager_id
    from   emp_dept e1
    where  e1.employee_id = 202
    
    union 
    
    select e2.employee_id , e2.manager_id
    from   p
    join   emp_dept e2 ON e2.employee_id = p.manager_id
    
    )
    select manager_id
      from   p
    

提交回复
热议问题