1

我需要做一个查询,计算每个部门中有多少员工拥有超过 1 名员工并且员工人数为 6 或更多,我有:

 Select DepartmentName, count(EmployeeNumber) as "Number of Employees"
 From department d, employee e
 Where d.DepartmentName = e.Department and e.EmployeeNumber <=6 
 Group by d.DepartmentName;

这满足了所有要求,除了关于拥有超过 1 名员工的部分,有没有办法只为从第一行返回 2 或更大的结果执行其余代码?

4

2 回答 2

2

使用HAVING从句。但请注意,它是在计算整个事物之后应用的。

Select DepartmentName, count(EmployeeNumber) as "Number of Employees"
From department d, employee e
Where d.DepartmentName = e.Department and e.EmployeeNumber <=6 
Group by d.DepartmentName HAVING count(EmployeeNumber) > 1;
于 2013-03-09T17:54:37.307 回答
1

尝试这个

    Select DepartmentName, count(EmployeeNumber) as "Number of Employees"
    From department d, employee e
    Where d.DepartmentName = e.Department  
    Group by d.DepartmentName and e.EmployeeNumber <=6
    HAVING count(EmployeeNumber) > 1 ;
于 2013-03-09T17:56:15.047 回答