每位员工有多少经理?

时间:2014-02-28 07:08:06

标签: sql sql-server

我得到了这个查询,为每位员工选择经理:

SELECT e1.Name EmployeeName, ISNULL(e2.name, 'Top Manager') AS ManagerName
FROM Employee e1
LEFT JOIN Employee e2
ON e1.ManagerID = e2.EmployeeID

我想为每位员工提供经理人数。所以例如Mike有1(Roger)和Marry有2(David + Roger)。我怎样才能获得每一行的这个数字?

dbscript:

USE TempDb
GO
-- Create a Table
CREATE TABLE Employee(
EmployeeID INT PRIMARY KEY,
Name NVARCHAR(50),
ManagerID INT
)
GO
-- Insert Sample Data
INSERT INTO Employee
SELECT 1, 'Mike', 3
UNION ALL
SELECT 2, 'David', 3
UNION ALL
SELECT 3, 'Roger', NULL
UNION ALL
SELECT 4, 'Marry',2
UNION ALL
SELECT 5, 'Joseph',2
UNION ALL
SELECT 7, 'Ben',2
GO

7 个答案:

答案 0 :(得分:1)

请尝试使用CTE:

;WITH T AS
(
    SELECT * FROM Employee 
    WHERE ManagerID IS NOT NULL
    UNION ALL
    SELECT a.* FROM T
        INNER JOIN Employee a ON a.ManagerID =  T.EmployeeID
)
SELECT 
    Name, 
    COUNT(*) ManagerNumber 
FROM T 
GROUP BY Name
ORDER BY 2, 1

答案 1 :(得分:0)

SELECT e1.Name EmployeeName, count(NULLIF(e2.name, 'Top Manager')) Count_Manager
FROM Employee e1
LEFT JOIN Employee e2
ON e1.ManagerID = e2.EmployeeID
GROUP BY e1.Name

这是伪代码,所以试一试。

答案 2 :(得分:0)

你去......试试这个......

;with CTE1
as
(
    SELECT 0 AS MANAGERCOUNT,EmployeeID,MANAGERID,Name FROM #Employee WHERE MANAGERID IS NULL
    UNION ALL
    SELECT C.MANAGERCOUNT+1 AS MANAGERCOUNT,E.EmployeeID,E.MANAGERID,E.Name
    FROM #Employee E INNER JOIN CTE1 C ON C.EmployeeID = E.MANAGERID
)

SELECT * FROM CTE1

答案 3 :(得分:0)

试试这个,

;WITH    CTE
          AS ( SELECT   * ,
                        0 lvl
               FROM     @employee
               WHERE    ManagerID IS NULL
               UNION ALL
               SELECT   a.* ,
                        CASE WHEN a.ManagerID = b.EmployeeID THEN 1 + lvl
                             ELSE lvl
                        END
               FROM     @Employee a
                        INNER JOIN cte b ON a.ManagerID = b.EmployeeID
             )
    SELECT  EmployeeID ,
            name ,
            ManagerID ,
            lvl AS [No Of manager]
    FROM    cte

答案 4 :(得分:0)

试试这个

-- Create a Table 
CREATE TABLE employee 
    ( 
         employeeid INT PRIMARY KEY,name NVARCHAR(50),managerid INT 
    ) 

go 

-- Insert Sample Data 
INSERT INTO employee 
SELECT 1,'Mike',3 
UNION ALL 
SELECT 2,'David',3 
UNION ALL 
SELECT 3,'Roger',NULL 
UNION ALL 
SELECT 4,'Marry',2 
UNION ALL 
SELECT 5,'Joseph',2 
UNION ALL 
SELECT 7,'Ben',2 

go 

WITH cte 
         AS (SELECT employeeid,name,managerid,Cast('Top Manager' AS NVARCHAR(50)) AS 
                                                                                         ManagerName,0 
                                        LVL 
                 FROM   employee 
                 WHERE  managerid IS NULL 
                 UNION ALL 
                 SELECT e.employeeid,e.name,e.managerid,c1.name ManagerName,c1.lvl + 1 
                 FROM   cte C1 
                                JOIN employee e 
                                    ON c1.employeeid = e.managerid) 
SELECT name,managername,lvl 
FROM   cte 

答案 5 :(得分:0)

假设您还希望向顶级经理' Roger'在结果集中:

WITH CTE AS
(
    SELECT * FROM Employee 
    WHERE ManagerID IS NOT NULL
    UNION ALL
    SELECT a.EmployeeID, a.Name, a.ManagerID FROM CTE
        INNER JOIN Employee a ON a.ManagerID =  CTE.EmployeeID
)
SELECT Name, COUNT(*) COUNT_MANAGERS FROM CTE
GROUP BY Name
UNION ALL
SELECT Name, (SELECT 0) FROM Employee WHERE ManagerID IS NULL
GROUP BY Name
ORDER BY COUNT_MANAGERS, Name

答案 6 :(得分:0)

{{1}}