我有一个数据集,可以根据公司的级别提取公司的员工层次结构。查询使用如下:
;WITH DirectReports (ManagerID, EmployeeID, Level)
AS
(
SELECT [managerid] , e.EmployeeID,
0 AS Level
FROM Resource AS e
WHERE e.[managerid] is null and e.statecode=0
UNION ALL
SELECT e.[mtctb_resourcemanageridname], e.EmployeeID, Level + 1
FROM Resource AS e
INNER JOIN Resource AS d
ON e.[managerid] = d.EmployeeID
join DirectReports AS dr on e.[managerid] = dr.EmployeeID
where e.statecode=0
)
SELECT ManagerID, EmployeeID, Level
FROM DirectReports
WHERE EmployeeID is not null