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 に答える