简体   繁体   English

如何选择在项目上工作时间最长的员工

[英]How to select the employee who worked the longest hours on project

In the following table I found the two projects that are over budget but I must find the employee number of the person that worked the most hours on each project: 在下表中,我发现了两个超出预算的项目,但我必须找到每个项目工作时间最长的人员的员工编号:

+------------+-----------+----------------+-------------+
| Department | ProjectID | EmployeeNumber | HoursWorked |
+------------+-----------+----------------+-------------+
| Marketing  |      1000 |              1 |       30.00 |
| Marketing  |      1300 |              1 |       35.00 |
+------------+-----------+----------------+-------------+

The correct employeenumber should be 8 for both projects and 75 and 80 hours. 两个项目的正确雇员人数应为8,75和80小时。

Any idea how to retrieve the MAX hours worked for these individual projects? 知道如何检索这些单独项目的MAX小时数吗?

The easy way is like this: 简单的方法是这样的:

select * from (
    select ProjectID, EmployeeNumber
    from assignment
    group by 1, 2
    order by sum(HoursWorked) desc
) x
group by 1

See live demo on SQLFiddle. 查看SQLFiddle上的现场演示

This works due to mysql's special handling of group by, which allows not all non-aggregate columns to be listed and in such cases returns just the first row encountered for each group. 这是因为mysql对group by的特殊处理,它不允许列出所有非聚合列,在这种情况下只返回每个组遇到的第一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM