Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grouping Query using Max function

Status
Not open for further replies.

cantona

Technical User
May 20, 2001
121
GB
I have a basic query which lists the courses that each team member is currently attending. There is more than one row per person as many are attending more than one course. What I would really like to do is display each person once, and only display the course details for the course that takes up the most hours. For Example

PersonID, CrsCode, Hours
123 ABC1 26
123 DEF1 33

I would want to display this as;

123 DEF 33

Ive tried grouping on personID and using the Max of Hours, but this wont then display the Crs Code.

I know this is probably something simple! But its been a really long day!! Any suggestions??
 
Get the max in a subquery and join it back into the main query, something like:
Code:
SELECT T2.PersonID, T2.CrsCode, T2.Hours
FROM Table2 T2,
(SELECT Table2.PersonID, max(Table2.Hours) as MostHours
FROM Table2
group by Table2.PersonID) MaxHours
Where T2.PersonID = MaxHours.PersonId
and T2.Hours = MaxHours.MostHours
Hope this helps

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top