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

Retrieve top score for each city 1

Status
Not open for further replies.

redaccess

MIS
Aug 2, 2001
110
0
0
US
ID City Score
1 Ada 100
2 Ada 95
3 Globe 91
4 Globe 96

Using the sample above, I'm trying to only retrieve the records for each city with the top score. In this case that's obviously ID#'s 1 & 4.

It' easy enough to use a Max function while grouping on the city but there are other fields in this table that I would like displayed as well such as Date, Address ect. ect.

I'm sure there's a simple solution to this but I'm just not seeing it.

Thanks.
 
Code:
select * from t as q
where score in (select max(score) from t
where t.city = q.city)
 
That worked great.

Let's try taking it one step further:

select ID, city, score, date from t as q
where score in (select max(score) from t
where t.city = q.city)

The above queries output:

ID City Score Date
1 Ada 100 1/1/2003
6 Ada 100 12/12/2003
4 Globe 96 1/1/2003

What would need to be added to the query to only retrieve those records with the most recent date?



 
Code:
select ID, city, score, date from t as q
where score in (select max(score) from t
where t.city = q.city)
and date = (select max(date) from t
where t.city = q.city)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top