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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query Question: finding a min value with a group

Status
Not open for further replies.

spud101

Programmer
Nov 13, 2002
9
CA
I have a race database that contains a table with a field for a lap time and another for the track where the lap was run, and a driver_id field for the driver. I'm trying to use a query to pull the fastest lap time for each track. I can get the info like this :

select min(qualifytime),Track
from points
group by Track

but i also need to see the driver who ran the lap.
If I add the driver_id to the query, I also have to add it to the group by and I get every drivers time, not just the driver with the fastest lap at each track.

I tried a where clause with a subquery like
where qualifytime = (select min(qualifytime) from points)
but that doesn't give a min time for each track.

Any help appreciated

 
Try this:

select b.track,b.qualtime,a.riderid from points a join (
select track,min(qualtime) qualtime from points
group by track) b
on a.track = b.track and a.qualtime = b.qualtime


Hope this helps.
 
Very Good!
Now all I have to do is pick it apart and try to understand how it works.

Thanks
 
The statement after the join basically creates an alias "table" with appropriate fields and then you join them to your original data to get your results.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top