Possible Duplicate:
Fetch the row which has the Max value for a column
My table looks like the following:
Sales
- id
- country_id
- location_id
- order_id
- created
Now I want only a single row to be returned (the latest, which can be either using the highest id value or using the datetime field created) for each country_id, location_id pair.
Is this possible in a single query?
This is a mysql-only solution, but it works because when you don’t aggregate the mon group-by columns, mysql gives you the first row it finds for each group, and if you order the rows before grouping, you get the tow you want.