user2820372
user2820372

Reputation:

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.

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?

Upvotes: 2

Views: 707

Answers (1)

Twelfth
Twelfth

Reputation: 7180

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.

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. Logic is simple...use a subquery to locate projectID and max hoursworked, inner join this back to the employee/assignment query. This functions as a filter leaving you with only the max hours worked by 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. Most other engines would flat out return an error saying that employeeID isn't part of the group by statement and fail right there. 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)

Edit:

That would have worked if we were not in 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

Upvotes: 1

Related Questions