我是新手,我不知道该如何解决这个问题..任何人都可以帮助我吗?
鉴于以下表格......
部门
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
... ... ... ... ...
使用前面的DEPARTMENT
和EMPLOYEE
表,编写SQL查询以打印每个部门中工作的员工数。输出应该如下所示:
NAME COUNT
HR 2
Technical 2
Marketing 0
Sales 0
请注意,并非所有部门都配备了人员,因此有些部门可能没有任何员工。我们仍然希望这些部门出现在结果中,计数为零。
使用前面的EMPLOYEE
表,编写SQL查询以打印出在每个经理下工作的员工数量。输出应该如下所示:
NAME COUNT
Alex 2
Sally 0
答案 0 :(得分:1)
您需要按部门加入表格,并按部门计算行数 Try This Link
答案 1 :(得分: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
答案 2 :(得分:0)
试试这可能会对你有所帮助:
A)
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;