对于202和203之间的每个manager_id,显示每个经理监督的员工数量

时间:2017-10-18 19:29:02

标签: mysql sql sql-server oracle

对于202和203之间的每个manager_id

  • 显示每位经理监督的员工数量
  • 仅显示管理2名以上员工的经理。

表信息:

CREATE TABLE Employee (  
    Employee_ID     INT     NOT NULL,  
    First_Name      VARCHAR(64) NOT NULL,  
    Last_Name       VARCHAR(64) NOT NULL,  
    Dept_Code       VARCHAR(6)  NOT NULL,  
    Hire_Date       DATE        NULL,  
    Credit_Limit    NUMERIC(8,2)    NULL,  
    Phone_Ext       INT     NULL,  
    Manager_ID      INT     NULL);  

INSERT INTO Employee VALUES (201,'Susan','Brown','Exe','01-Jun-1998',30.00,3484,NULL);  
INSERT INTO Employee VALUES (202,'Jim','Kern','Sal','16-Aug-1999',25.00,8722,201);  
INSERT INTO Employee VALUES(203,'Martha','Woods','Shp','02-Feb-2004',25.00,7591,201);  
INSERT INTO Employee VALUES(204,'Ellen','Owens','Sal','01-Jul-2003',15.00,6830,202);  
INSERT INTO Employee VALUES(205,'Henry','Perkins','Sal','01-Mar-2000',25.00,5286,202);  
INSERT INTO Employee VALUES(206,'Carol','Rose','Act','','',NULL,NULL);  
INSERT INTO Employee VALUES(207,'Dan','Smith','Shp','01-Dec-2004',25.00,2259,203);  
INSERT INTO Employee VALUES(208,'Fred','Campbell','Shp','01-Apr-2003',30.00,1752,203);  
INSERT INTO Employee VALUES(209,'Paula','Jacobs','Mkt','17-Mar-1999',15.00,3357,201);  
INSERT INTO Employee VALUES(210,'Nancy','Hoffman','Sal','16-Feb-2004',25.00,2974,203); 

2 个答案:

答案 0 :(得分:1)

我建议阅读基本的sql选择和分组。这样的事情应该有效:

SELECT manager_id
    ,count(distinct employee_id) employees_managed
from employee
where manager_id in (202, 203)
group by 1
having count(distinct employee_id) > 2

你基本上想要为每个经理计算员工ID。并过滤经理ID。然后筛选大于2的员工数。

答案 1 :(得分:0)

以下是您将得到答案的方法:

    SELECT Manager_ID, COUNT(Employee_ID) AS EmployeeCount
    FROM dbo.Employee
    WHERE Manager_ID BETWEEN 202 AND 203
    GROUP BY Manager_ID
    HAVING COUNT(1) >2