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

后端 未结 2 1793
广开言路
广开言路 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
    
    0 讨论(0)
  • I think you can use "hierarchical queries" for Oracle and try this:

    select manager_id supervisor 
    from employee
    start with employee_id = 202
    connect by nocycle employee_id = prior manager_id;
    
    0 讨论(0)
提交回复
热议问题