Returning extra columns in a GROUP BY
Greetings,
I have what appears to be a deceptively simple question. I have a table(Jobs) with a number of columns in it(10). I am only interested in 4 of those columns. They would be:
Project,
Employee_ID,
Job_Name,
Job_Start_Date
This table could contain, for these 4 columns:
'Beach', 'Emp01', 'Drive', 2009-04-01
'Beach', 'Emp01', 'Unpack', 2009-04-02
'Beach', 'Emp01', 'Blanket', 2009-04-03
'Beach', 'Emp02', 'Talk', 2009-05-02
'Beach', 'Emp03', 'Surf', 2009-06-14
'Beach', 'Emp03', 'BreakLeg', 2009-06-20
There could be more than one Project, but for this question and for sake of size, I have given only 1. What I would like to do is to take this result set and then return from it, based on the Project and Employee_ID, the highest Job_Start_Date and it's corresponding Job_Name.
I can write a select to get the MAX(Job_Start_Date), but then how to include the Job_Name?
/*
Non-Working example to show progression and stopping point due to cluelessness.
** No need to say this SELECT is incorrect or bad **
*/
SELECT
Project,
Employee_ID,
Job_Name, -- Needs to be the same Job_Name from the MAX(Job_Start_Date) record.
MAX(Job_Start_Date)
FROM Jobs
GROUP BY Project, Employee_ID
The final result should be:
'Beach', 'Emp01', 'Blanket', 2009-04-03
'Beach', 'Emp02', 'Talk', 2009-05-02
'Beach', 'Emp03', 'BreakLeg', 2009-06-20
I hope that this simplified example still provides enough information to help find a solution to my problem. If you need more information, please let me know and I will try to give you more.
Thank you for your time and help.
Terry Steadman
|