2

我有一个这样的表结构:

employee
    id int
    manager_id int (the employee id of the manager)
    name
    ...

awards
    id int
    employee_id int
    points int (the award "value")

我如何找到其员工(直接向经理汇报的人员)集体奖励积分最多的经理?

4

2 回答 2

3

像这样:

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
于 2013-01-03T00:26:38.613 回答
2
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
于 2013-01-03T00:42:52.540 回答