SQL Query用于获取每个经理下的员工的递归计数

时间:2013-10-30 17:52:54

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

我正在寻找一个SQL查询来获取每位经理或其他表中某些经理的递归计数Employee

表结构是:

EMP_TABLE

Emp_id Manager_id
1         1
2         1
3         1
4         3
5         3
6         5
7         5
8         7
9         7
10        7

执行表:

此表只是选定的经理可以在这里找到。我需要在这位经理下获得每位员工的数量

Manager_id
     1
     4
     7
     8

我正在寻找以下输出

Manager_ID   Count_of_Employees
     1             9
     4             0
     7             3 
     8             0

请注意,您的答案仅为每位经理提供直接报告员工人数。我正在寻找经理的递归员工报告。在上面的示例中,根据您的查询,我将得到Manager_id 1计数为3,但我正在寻找输出是9.提前感谢!

感谢您的回复。我终于得到了查询。请找到我的答案。

11 个答案:

答案 0 :(得分:4)

首先,重要提示Emp_Table的第一行,其中Emp_id==Manager_Id==1 不仅无意义,还会导致无限递归 即可。我建议你删除它。

但是,为了提供答案,我首先创建了一个消除此类无效条目的视图,并使用 代替Emp_Table

create view valid_mng as 
select Emp_Id,Manager_id from Emp_Table
where Emp_Id<>Manager_Id

因此,在递归CTE的帮助下,归结为以下内容:

With cte as (
  select Emp_Id,Manager_id from valid_mng
  union all
  select c.Emp_Id,e.Manager_Id 
  from cte c join valid_mng e on (c.Manager_Id=e.Emp_Id)
  )

select m.Manager_Id,count(e.Emp_Id) as Count_of_Employees
from [Execute] m
left join cte e on (e.Manager_Id=m.Manager_Id)
group by m.Manager_Id

如果您最终删除了违规行,或者更好地将Manager_Id=NULL设置为HABO建议,则忽略valid_mng视图并将其替换为{{1到处都是。

另外注意:Emp_Table是MSSQL中的保留字。避免在用户对象命名中使用保留字是始终是一个好习惯

答案 1 :(得分:4)

与地瘤相似,但不是由此衍生而来。它以递归方式构建管理报告树,然后对其进行汇总。

declare @Emp_Table as Table ( Emp_id Int Identity, Manager_id Int );
insert into @Emp_Table ( Manager_id ) values
  ( 1 ), ( 1 ), ( 1 ), ( 3 ), ( 3 ), ( 5 ), ( 5 ), ( 7 ), ( 7 ), ( 7 );
select * from @Emp_Table;

declare @Execute as Table ( Manager_id Int );
insert into @Execute ( Manager_id ) values
  ( 1 ), ( 4 ), ( 7 ), ( 8 );
select * from @Execute;

with Reports as (
  select Manager_id, Emp_id, Manager_id as Top_Manager_id
    from @Emp_Table where Manager_id <> Emp_id
  union all
  select ET.Manager_id, ET.Emp_id, R.Top_Manager_id
    from Reports as R inner join
      @Emp_Table as ET on ET.Manager_id = R.Emp_id
  )
  select E.Manager_id, Count( R.Emp_id ) as Count_Of_Employees
    from @Execute as E left outer join
      Reports as R on R.Top_Manager_id = E.Manager_id
    group by E.Manager_id;

答案 2 :(得分:3)

您可以使用分组依据:

SELECT count(*) AS Count_of_Employees,
    Manager_ID
FROM yourtable
GROUP BY Manager_ID

答案 3 :(得分:2)

我遇到了这样的问题,找到属于高层经理的员工数量

表格:

CREATE TABLE employees (
  id INTEGER PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  managerId INTEGER,
  FOREIGN KEY (managerId) REFERENCES employees(id)
);

INSERT INTO employees(id, name, managerId) VALUES(1, 'Rob', null);
INSERT INTO employees(id, name, managerId) VALUES(2, 'Tom', null);
INSERT INTO employees(id, name, managerId) VALUES(3, 'Roger', 1);
INSERT INTO employees(id, name, managerId) VALUES(4, 'Victor', 3);
INSERT INTO employees(id, name, managerId) VALUES(5, 'John', 4);
INSERT INTO employees(id, name, managerId) VALUES(6, 'Shawn', 2);

-- Expected output
-- Id    EmployeeCount
-- --------------------
-- 1          3
-- 2          1

我的解决方案是:

WITH EmployeeCTE
     AS (SELECT e.id, e.managerid as topmanager
         FROM   employees e, employees m
         WHERE  e.managerid = m.id AND m.managerid IS NULL
         UNION ALL
         SELECT e.id, mgr.topmanager
         FROM   employees e, EmployeeCTE mgr
         WHERE  e.managerid = mgr.id)
SELECT topmanager, count(*)
FROM   EmployeeCTE AS u
GROUP BY topmanager;

答案 4 :(得分:1)

您可能需要尝试以下操作:

SELECT MANAGER_ID,
    COUNT(EMP_ID) AS EMP_COUNT
FROM EMP_TABLE
GROUP BY MANAGER_ID

答案 5 :(得分:1)

SELECT Manager_id, COUNT(Emp_id) AS Count_of_Employees
FROM Emp_Table
WHERE Manager_id IN (1,4,7,8)
GROUP BY Manager_id

答案 6 :(得分:0)

SELECT manager_id,
    count(DISTINCT emp_id)
FROM emp_table
GROUP BY manager_id

UNION

SELECT manager_id,
    0
FROM execute_table
WHERE manager_id NOT IN (
        SELECT manager_id
        FROM emp_table
        )

答案 7 :(得分:0)

SELECT Manager_Id,
    Count(Employee_Id)
FROM Emp_Table
WHERE Manager_Id IN (
        SELECT Manager_Id
        FROM Manager
        )
GROUP BY Manager_Id

答案 8 :(得分:0)

SELECT COUNT(Emp_id) county,
    Manager_id
FROM Emp_Table
GROUP BY Manager_id

答案 9 :(得分:-1)

我的回答...感谢您的帮助。

WITH Emp_Count as
(
Select Emp_id boss,Emp_id from Emp_Table t
Where exists (select 1 from Emp_Table Where t.emp_id = Manager_id)
UNION all
Select Emp_Count.boss, t.emp_id
from Emp_Table t join Emp_Count on t.Manager_id = Emp_Count.emp_id
)
Select boss, count(*)-1 SubCount 
from Emp_Count 
group by boss
option (maxrecursion 0)

答案 10 :(得分:-1)

这是一个简单的pl sql查询,用于获取经理下的员工总数(直接和间接)。

select empno , ename, 
(
select count(empno) from emp
start with ename = a.ename
connect by prior empno = mgr
) as under 
from emp a;