Employee Manager Report - Sql Server

后端 未结 4 1283
借酒劲吻你
借酒劲吻你 2021-01-28 21:14

I have the below table

empid   empname     managerID
1          A          NULL
2          B           1
3          C           1
4          D           2
         


        
4条回答
  •  情话喂你
    2021-01-28 21:34

    Self join the table

    case 1: all employees who have managers

    select e1.empname employee, e2.empname manager
    from employee e1
    join employee e2 on e1.managerId = e2.empid
    order by e2.name,e1.name
    

    case 2: all employees who have managers or not

    select e1.empname employee, COALESCE(e2.empname,'none') manager
    from employee e1
    left join employee e2 on e1.managerId = e2.empid
    order by e2.name,e1.name
    

提交回复
热议问题