Employee sql的高级经理

时间:2017-11-02 05:57:50

标签: sql sql-server sql-server-2008

我有一张员工表:

Employee

我有一个类别表,它连接了userid和category id:

enter image description here

值为1的类别是高级经理。

我想找到每个员工的高级经理。类别值为1的高级经理。

我需要这样的输出:

enter image description here

我们如何在SQL Server 2008中实现这一目标?

任何帮助表示感谢。

2 个答案:

答案 0 :(得分:0)

由于此查询需要ResourceFile,因此应该可以使用:

 SELECT a.EMPLOYEENAME as Employee,
        b.EMPLOYEENAME as Senior_Manager
 FROM employee a
 LEFT JOIN
 employee b ON a.ID = b.Manager
 LEFT JOIN
 category c ON b.ID = c.UserID
 WHERE c.Category = 1

答案 1 :(得分:0)

尝试:

WITH Emp_CTE AS (

 Select ID,EmployeeName,Manager from employee

 UNION ALL

 SELECT ecte.ID,ecte.EmployeeName,c.Manager
 FROM employee c
 INNER JOIN Emp_CTE ecte ON ecte.Manager = c.ID
)
SELECT a.ID,a.EmployeeName,b.EmployeeName
FROM Emp_CTE a
left join employee b
on a.Manager = b.ID
left join category c
on a.Manager = c.UserID
where c.Category = '1'