1

我是新手,我不知道该怎么做..有人可以帮助我吗?

鉴于下表...

DEPT_ID     NAME 
1   HR 
2   Technical 
3   Marketing 
4   Sales 

员工

ID  NAME    DEPT_ID     SALARY  MANAGER_ID 
1   Alex    2   2000    
2   Sally   1   2000    
3   Amit    2   1500    1 
4   Jason   1   1500    1 
...     ...     ...     ...     ... 

使用前面的DEPARTMENTEMPLOYEE表,编写一个 SQL 查询来打印在每个部门工作的员工人数。输出应如下所示:

NAME    COUNT 
HR  2 
Technical   2 
Marketing   0 
Sales   0 

请注意,并非所有部门都已配备人员,因此某些部门可能没有任何员工。我们仍然希望这些部门以零计数出现在结果中。

使用EMPLOYEE之前的表,编写一个 SQL 查询来打印出在每个经理手下工作的员工数量。输出应如下所示:

NAME    COUNT 
Alex    2 
Sally   0 
4

3 回答 3

1

您需要加入表格,按部门分组,并计算每个部门的行数。
试试这个链接

于 2012-04-27T19:58:23.593 回答
0

解决第一部分问题:

select d.NAME, count(e.NAME)
from EMPLOYEE as e
right join DEPARTMENT as d
on e.DEPT_ID = d.DEPT_ID
group by d.DEPT_ID

问题第二部分的解决方案(我不确定这个是否正确):

select NAME, count(NAME)
from EMPLOYEE 
group by MANAGER_ID

请检查 sqlfiddle:http ://sqlfiddle.com/#!2/5e802/13

于 2014-03-18T07:35:43.200 回答
0

试试这个它可以帮助你:

一个)

 select d.Name as DepartmentName, count(e.id) as EmployeeCount from employee as e, department as d where e.DEPT_ID = d.DEPT_ID group by d.Name;

b)

select a.name as Manager_Name,Count(a.name) as EmployeeCount from employee a, employee b where a.id=b.MANAGER_ID  group by a.name;
于 2016-06-24T12:25:40.067 回答