sql - 集体最大值

标签 sql

我有一个这样的表结构:

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

关于sql - 集体最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14131253/

相关文章:

c# - Linq 查询以列形式返回国家/地区,以行形式返回城市并在单元格中计数

sql - Postgres GROUP BY 查看日期范围

ASP.NET : select multiple values in query using Linq

python - python 中 mySQL 的最后一个值(查询)

sql - Liquibase 脚本使列中的所有数据都大写

sql - 动态枢轴sql

sql - JSON 和 SQL 之间的区别

mysql - 寻找大表查询的优化

sql - 如何从两列的值中选择 DISTINCT

mysql - 针对海量数据优化MySQL Full outer join