SQL层次结构和连接表计数

时间:2013-03-21 21:37:18

标签: sql recursion

我有一个Employee Table和一个Projects表。员工只能在1个项目中,并将员工表作为具有ID和ReportsTO ID的层次结构。我需要得到管理人员计算的项目数量,这些项目将是他下属员工的总数等等......

    DECLARE @Employees TABLE (EmpID INT, EmpName VARCHAR(20), ReportsTo INT)
INSERT INTO @Employees(EmpID, EmpName, ReportsTo)
  SELECT 1, 'Jacob', NULL UNION ALL
  SELECT 2, 'Rui', NULL UNION ALL
  SELECT 3, 'Jacobson', NULL UNION ALL
  SELECT 4, 'Jess', 1 UNION ALL
  SELECT 5, 'Steve', 1 UNION ALL
  SELECT 6, 'Bob', 1 UNION ALL
  SELECT 7, 'Smith', 2 UNION ALL
  SELECT 8, 'Bobbey', 2 UNION ALL
  SELECT 9, 'Steffi', 3 UNION ALL
  SELECT 10, 'Bracha', 3 UNION ALL
  SELECT 11, 'John', 5 UNION ALL
  SELECT 12, 'Michael', 6 UNION ALL
  SELECT 13, 'Paul', 6 UNION ALL
  SELECT 14, 'Lana', 7 UNION ALL
  SELECT 15, 'Johnson', 7 UNION ALL
  SELECT 16, 'Mic', 8 UNION ALL
  SELECT 17, 'Stev', 8 UNION ALL
  SELECT 18, 'Paulson', 9 UNION ALL
  SELECT 19, 'Jessica', 10

DECLARE @Projects Table (ID INT,EMPID INT, PROJNAME VARCHAR(50))
INSERT INTO @PROJECTS(ID,EMPID,PROJNAME)  
    SELECT 1,19,'ABCD' UNION ALL
    SELECT 2,5,'EFGH'   UNION ALL

    SELECT 3,16,'ADAD'  UNION ALL

    SELECT 4,6,'FSDFSD' UNION ALL
    SELECT 6,8,'DSDSDSD' UNION ALL
    SELECT 6,8,'DSDSADDSD'



--Solution starts here
DECLARE @manager VARCHAR(20)
SELECT @manager ='JACOB'

--CTE
;With Hierarchy(EmpName, EmpID, Level, FullyQualifiedName)
As
(
  Select E.EmpName, E.EmpID, 0, Cast('.'+E.EmpName+'.' as Varchar(MAX))
    From @Employees E
    Where E.ReportsTo is null
  Union all
  Select E.EmpName, E.EmpID, H.Level+1, H.FullyQualifiedName+'.'+E.EmpName+'.'
    from @Employees E
    inner join Hierarchy H on H.EmpID=E.ReportsTo
)

--Result
Select Space((Level-(Select Top(1) Level from Hierarchy H2 Where H2.EmpName=@manager))*4) + EmpName
  from Hierarchy H
  where CHARINDEX('.'+(Select Top(1) E.EmpName from @Employees E Where E.EmpName=@manager)+'.', H.FullyQualifiedName) > 0
  order by H.FullyQualifiedName

如何获得每位员工的项目计数?

1 个答案:

答案 0 :(得分:0)

管理人员汇总所有员工的项目数量。

--Result
Select Space((Level-(Select Top(1) Level from Hierarchy H2 Where H2.EmpName=@manager))*4) + EmpName,
       COALESCE(CASE WHEN ID IS NOT NULL THEN 1 END,
       CASE WHEN Level = 0 THEN SUM(CASE WHEN ID IS NOT NULL THEN 1 END) OVER() END, 0) AS cnt
from Hierarchy H LEFT JOIN @PROJECTS p ON H.EMPID = p.EMPID
where CHARINDEX('.'+(Select Top(1) E.EmpName from @Employees E Where E.EmpName=@manager)+'.', H.FullyQualifiedName) > 0
order by H.FullyQualifiedName

SQLFiddle上的演示

或试试这个

--Result
Select Space((Level-(Select Top(1) Level from Hierarchy H2 Where H2.EmpName=@manager))*4) + EmpName,       
       CASE WHEN Level = 0 THEN SUM(CASE WHEN ID IS NOT NULL THEN 1 END) OVER() END AS totalCnt,
       CASE WHEN ID IS NOT NULL THEN 1 END AS cntSingle,              
       o.cntChild
from Hierarchy H OUTER APPLY (
                              SELECT COUNT(p.PROJNAME) AS cntChild
                              FROM @Employees e LEFT JOIN @PROJECTS p ON e.EmpID = p.EMPID
                              WHERE h.EmpID = e.ReportsTo
                              ) o
                 LEFT JOIN @PROJECTS p ON H.EMPID = p.EMPID                           
where CHARINDEX('.'+(Select Top(1) E.EmpName from @Employees E Where E.EmpName=@manager)+'.', H.FullyQualifiedName) > 0
order by H.FullyQualifiedName