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.
Author |
Topic |
darms21
Yak Posting Veteran
54 Posts |
Posted - 2013-04-09 : 12:17:49
|
Hi -Table1:Name Date TotalK 2/25/12 10K 1/1/13 10K 1/2/13 12M 1/10/13 16M 1/11/13 16How can I create a statement that will identify all row where the Max date record for each Name has a greater total than the 2nd Max date?I know that the following will give me the MAX date for each NameSELECT Name, MAX(Date)FROM Table1 GROUP BY Name |
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2013-04-09 : 13:17:28
|
[code]WITH t AS ( SELECT name, date, total, row_number() over (partition by name order by date desc) ix FROM Table1)SELECT t1.name, t1.date, t1.totalFROM t t1JOIN t t2On t1.name = t2.nameAnd t1.ix = 1And t2.ix = 2WHERE t1.total > t2.total;[/code] |
|
|
|
|
|