Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 MAX function (This is an easy one!)

Author  Topic 

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2011-06-30 : 12:01:59
Hi members! I have the following simple table:

TEL_NUMBER INIT_DATE MODEL

4165551234 05/01/2011 NOKIA
4165551234 02/18/2011 MOTOROLA
4165551234 01/02/2011 SAMSUNG

4165553999 06/01/2011 ACER
4165553999 02/28/2011 APPLE
4165553999 01/19/2011 LG

I would like the Query to output just one distinct TEL_NUMBER and the MODEL based on the most recent INIT_DATE, so the output would be:

4165551234 05/01/2011 NOKIA
4165553999 06/01/2011 ACER

Can you please tell me what I'm missing here?

SELECT TEL_NUMBER, MAX(INIT_DATE), MODEL
FROM SOURCE_TABLE T1

Thanks!

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-06-30 : 12:09:35
You need to GROUP BY. This will give you the most recent init_date for each TEL_NUmber,Model combination

SELECT TEL_NUMBER, MAX(INIT_DATE), MODEL
FROM SOURCE_TABLE T1
GROUP BY TEL_NUMBER,MODEL


Jim


Everyday I learn something that somebody else already knew
Go to Top of Page
   

- Advertisement -