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
 Select one of two rows

Author  Topic 

KevinC
Starting Member

4 Posts

Posted - 2008-08-22 : 13:11:48
Hello,

This is my first time posting. I am new to SQl server and SQL.
I am trying to select one of two rows for a customerID from a table based on which row has a more recent date.

Both rows have the same key column(CustomerID). I tried 'Group By' with 'Having Max' but it didn't work. Can anyone help me please?!!

Thank you !!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-22 : 13:22:23
SELECT t.*
FROM YourTable t
INNER JOIN (SELECT customerID,MAX(datefield) AS MaxDate
FROM YourTable
GROUP BY customerID)t1
ON t.customerID=t1.customerID
AND t.datefield=t1.MaxDate
Go to Top of Page

KevinC
Starting Member

4 Posts

Posted - 2008-08-22 : 14:05:52
Thank you so much VSAKH16 !!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-23 : 01:16:58
quote:
Originally posted by KevinC

Thank you so much VSAKH16 !!


you're welccome
Go to Top of Page
   

- Advertisement -