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
 Dates

Author  Topic 

Mr Butter
Starting Member

3 Posts

Posted - 2012-07-07 : 14:53:30
Using Tsql I am looking to return the records where date1 is greater than date2 unless date2 is greater than any date1 entry.



Table

CaseNumber date1 date2
123 12/25/2011 12/30/2011
123 12/31/2011
456 12/25/2011 12/30/2011
456 12/29/2011



Results

CaseNumber date1 date2
123 12/31/2011
456 12/25/2011 12/30/2011




visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-07 : 16:29:53
[code]
SELECT CaseNumber,date1,date2
FROM
(
SELECT ROW_NUMBER() OVER PARTITION BY CaseNumber ORDER BY COALESCE(date2,date1) DESC) AS Seq,*
FROM Table
)t
WHERE Seq=1
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -