简体   繁体   English

选择工作时间最多的员工编号

[英]Select employee number who worked most hours

I am trying to choose the employee who worked the most hours on each project however it is just picking the first attribute for each projectID. 我试图选择在每个项目上花费最多时间的员工,但这只是为每个projectID选择第一个属性。

The assignment table: 分配表:

+-----------+----------------+-------------+
| ProjectID | EmployeeNumber | HoursWorked |
+-----------+----------------+-------------+
|      1000 |              1 |       30.00 |
|      1000 |              8 |       75.00 |
|      1000 |             10 |       55.00 |
|      1100 |              4 |       40.00 |
|      1100 |              6 |       45.00 |
|      1200 |              1 |       25.00 |
|      1200 |              2 |       20.00 |
|      1200 |              4 |       45.00 |
|      1200 |              5 |       40.00 |
|      1300 |              1 |       35.00 |
|      1300 |              8 |       80.00 |
|      1300 |             10 |       50.00 |
|      1400 |              4 |       15.00 |
|      1400 |              5 |       10.00 |
|      1400 |              6 |       27.50 |
+-----------+----------------+-------------+

And my view table to display the employee who worked the most hours on each project: 我的视图表显示了每个项目上工作时间最多的员工:

+----------------+-------+-----------+
| EmployeeNumber | MAX   | ProjectID |
+----------------+-------+-----------+
|              1 | 75.00 |      1000 |
|              4 | 45.00 |      1100 |
|              4 | 45.00 |      1200 |
|              1 | 80.00 |      1300 |
|              4 | 27.50 |      1400 |
+----------------+-------+-----------+

Any ideas as to why it's showing employee numbers 1, 4, 4, 1, 4 instead of 8, 6, 4, 8 ,6? 关于为什么显示雇员人数1、4、4、1、4而不是8、6、4、8、6的任何想法?

Not sure if either of the answers have this yet. 不知道两个答案中是否有这个。

Select project_id , max(hoursworked) 
FROM Assignment AS A JOIN Employee AS E
ON A.EmployeeNumber = E.EmployeeNumber
GROUP BY A.PROJECTID

This query tells you what the max hours worked is by project. 该查询告诉您项目最多可以工作的小时数。 Join that back to the query to get the employee ID that did that. 将其连接回查询以获取执行此操作的员工ID。

CREATE VIEW MostHours AS
SELECT E.EmployeeNumber, qry.hoursworked, ProjectID
FROM Assignment AS A JOIN Employee AS E
ON A.EmployeeNumber = E.EmployeeNumber
inner join 
   ( Select project_id , max(hoursworked) hoursworked
    FROM Assignment AS A JOIN Employee AS E
    ON A.EmployeeNumber = E.EmployeeNumber
    GROUP BY A.PROJECTID) qry
on a.projectID = qry.projectid and a.hoursworked = qry.hoursworked

I think thats right on grammar...sorry I don't have a mysql area to test it on. 我认为语法是正确的...抱歉,我没有mysql区域可以对其进行测试。 Logic is simple...use a subquery to locate projectID and max hoursworked, inner join this back to the employee/assignment query. 逻辑很简单...使用子查询查找projectID和最大工作时间,将其内部重新加入到员工/分配查询中。 This functions as a filter leaving you with only the max hours worked by projectID. 该功能用作过滤器,只剩下projectID可以工作的最大时数。 This method has the added benefit of returning two rows for a project when 2 people have equally worked the most hours. 这种方法的另一个好处是,当两个人平均工作时间最长时,将为项目返回两行。

Incidentally...the reason for the current behaviour you are seeing is entirely MYSQL. 顺便说一句...导致您看到当前行为的原因完全是MYSQL。 Most other engines would flat out return an error saying that employeeID isn't part of the group by statement and fail right there. 大多数其他引擎都会抛出错误,指出employeeID不属于group by语句,并在那里失败。 MYSQL for some reason decides it's OK and randomly brings back one of the employeeIDs (or whatever field wasn't properly specified in the group by cluase) 由于某种原因,MYSQL认为可以,并随机带回了一个employeeID(或在分组中未正确指定的任何字段)

Edit: 编辑:

That would have worked if we were not in MYSQL. 如果我们不使用MYSQL,那将行得通。 The work around is two views not one...See the link in my most recent comment: 解决方法是两种观点,而不是一种观点...请参阅我最近评论中的链接:

 Create view maxmosthours AS
    Select project_id , max(hoursworked) hoursworked
    FROM Assignment AS A JOIN Employee AS E
    ON A.EmployeeNumber = E.EmployeeNumber
    GROUP BY A.PROJECTID

CREATE VIEW MostHours AS
SELECT E.EmployeeNumber, qry.hoursworked, ProjectID
FROM Assignment AS A JOIN Employee AS E
ON A.EmployeeNumber = E.EmployeeNumber
inner join maxmosthours qry
on a.projectID = qry.projectid and a.hoursworked = qry.hoursworked

Thats one %^%@$% of a limitation in MYSQL 那就是MYSQL限制的一个%^%@ $%

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

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