1

我正在尝试编写一个查询来获取每个部门的员工人数。我正在使用内部查询进行此操作。我面临的问题是我需要过滤掉计数小于 10 的记录。这是我的查询的外观-

SELECT 
   DPT.ID, 
   DPT.NAME, 
   (SELECT COUNT(1) FROM EMPLOYEE E WHERE E.DEPARTMENT_ID=DPT.ID) EMP_COUNT
FROM 
   DEPARTMENT DPT
WHERE 
   EMP_COUNT >=10; -- this part is not working

谁能建议我可以代替的东西EMP_COUNT >=10 ?提前致谢。

4

3 回答 3

4

为什么要使用子查询?在大多数 RDBMS-es 上这可能会更快

SELECT     DPT.ID 
,          DPT.NAME 
,          COUNT(*) EMP_COUNT
FROM       DEPARTMENT DPT
INNER JOIN EMPLOYEE E
ON         DPT.ID = E.DEPARTMENT_ID
GROUP BY   DPT.ID 
,          DPT.NAME
HAVING     COUNT(*) >= 10;
于 2012-05-29T07:09:14.970 回答
1
SELECT DPT.ID, DPT.NAME, DEPT_EMP_COUNT
FROM DEPARTMENT DPT, (SELECT E.DEPARTMENT_ID , COUNT(1) AS DEPT_EMP_COUNT 
                      FROM EMPLOYEE E
                      GROUP BY E.DEPARTMENT_ID ) EMP_COUNT 
WHERE EMP_COUNT.DEPARTMENT_ID = DPT.ID AND DEPT_EMP_COUNT > 10; 
于 2012-05-29T07:02:16.867 回答
0

尝试这个

select dept.deptId,
dept.deptName,
count(emp.empid) as numb_of_emps
from department dept inner join employee emp
on dept.deptid = emp.deptid
group by dept.deptid,dept.deptName
having count(emp.empid) >= 10
于 2012-05-29T07:15:29.813 回答