我有三张桌子办公室,经理,工作人员。以下是这些表格的内容。
办公室
office_id office_location office_telephone office_fax
100 brisbane 01438263 789
101 newyork 01457899 978
102 chicago 01457989 789
管理器
office_id manager_id manager_name manager_phone
100 200 wayne 9879664878
101 201 tom 9745997669
102 202 harry 9789979799
员工
manager_id staff_id salary
200 300 3000
201 301 4000
200 302 5000
200 303 7856
201 304 4000
202 305 7856
202 306 6000
现在,我需要一个查询来显示每个办公室的员工及其经理的总数。
以下是示例输出
office_id office_location manager_id count(staff_id)
100 brisbane 200 3
101 newyork 201 2
102 chicago 202 2
直到现在我已经尝试过显示manager_id和为他们工作的员工。
SELECT manager_id,count(staff_id) from staff group by manager_id;
但我无法显示office_id及其位置。有人可以帮帮我吗?
答案 0 :(得分:0)
内部查询首先获得每位经理的计数和总工资......然后,按照链条进行操作以获得其他细节
SELECT
s.manager_id,
m.manager_name,
m.manager_phone,
o.office_location,
o.office_telephone,
o.office_fax,
s.NumOfEmployees,
s.AllSalary
from
( select s1.manager_id,
count(*) as NumEmployees,
SUM( s1.salary ) as AllSalary
from staff s1
group by s1.manager_id ) s
join manager m
ON m.manager_id = m.manager_id
join office o
ON m.office_id = o.office_id
答案 1 :(得分:0)
SELECT o.office_id,
o.office_location,
m.manager_id,
count(*) staff_count
from staff s
inner join manager m
on m.manager_id = s.manager_id
inner join office o
on o.office_id = m.office_id
group by o.office_id,
o.office_location,
m.manager_id;
答案 2 :(得分:0)
我刚刚创建了您上面提到的表格和数据,如果您只想要输出中提到的列,您可以使用以下内容:
SELECT O.office_id
,office_location
,M.manager_id
,COUNT(staff_id)
FROM Staff S
INNER JOIN Manager M
ON S.manager_id = M.manager_id
INNER JOIN Office O
ON O.office_id = M.office_id
GROUP BY M.manager_id
,O.office_id
,O.office_location