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

MAX from grouping 1

Status
Not open for further replies.

micang

Technical User
Aug 9, 2006
626
US
SQL 2005

Hi All,

I have a table with the following data:

ID,Make
1,Ford
2,Ford
3,Ferrari
4,Ferrari
5,Ferrari
6,Ferrari
7,Mazda
8,Renault
9,Renault
10,Renault

ID column is a primary key.

The result I world ideally like from the above is:

ID,Make
2,Ford
6,Ferrari
7,Mazda
10,Renault



I have tried with the code below, but this returns all the ID's.

Code:
SELECT     MAX(ID) AS Expr1, make
FROM         table1
GROUP BY ID, Make

Can't seem to think how to go about this, I am sure it's probably simple enough, I just can't see it, yet.

Any help in the correct direction will be appreciated.

Thank you
Michael
 
You want to AGGREGATE one column, GROUPING BY another. So your query needs to look like this:

Code:
SELECT     MAX(ID) AS Expr1, make
FROM         table1
GROUP BY Make
 
Thanks RiverGuy, silly me, grouping by ID (primary key), obviously that will return all the records.

Appreciate your time.

Michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top