1

我有一个员工表和一个项目表。员工只能在 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

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

4

1 回答 1

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
于 2013-03-21T22:40:26.847 回答