我有一个这样的表结构:
employee
id int
manager_id int (the employee id of the manager)
name
...
awards
id int
employee_id int
points int (the award "value")
我如何找到其员工(直接向经理汇报的人员)集体奖励积分最多的经理?
我有一个这样的表结构:
employee
id int
manager_id int (the employee id of the manager)
name
...
awards
id int
employee_id int
points int (the award "value")
我如何找到其员工(直接向经理汇报的人员)集体奖励积分最多的经理?
像这样:
SELECT
mgr.id,
mgr.name,
SUM(awd.points) As TotalStaffPoints
FROM employee As mgr
JOIN employee As stf ON mgr.id = stf.manager_id
JOIN awards As awd ON stf.id = awd.employee_id
GROUP BY mgr.id, mgr.name
ORDER BY TotalStaffPoints DESC
select top 1 employee.manager_id, SUM(awards.points) as total
from employee
join awards on employee.id = awards.employee_id
group by employee.manager_id
order by SUM(awards.points) desc