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 |
|
Vadivu
Starting Member
31 Posts |
Posted - 2009-09-23 : 05:56:01
|
| I have a history table where records are stored as followsCode EffDate6789 9/23/20096789 9/20/20096789 8/21/20096789 7/14/20091353 9/23/20091353 9/20/20091353 8/14/20091353 8/7/2009how to write a query to get the recent 3 effdates for each record?I need an output as followsCode EffDate6789 9/23/20096789 9/20/20096789 8/21/20091353 9/23/20091353 9/20/20091353 8/14/2009Can anybody help me? thanks in advance. |
|
|
Vadivu
Starting Member
31 Posts |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2009-09-23 : 06:57:22
|
| This method is somewhat old...here is a newer version compatible with 2005 and above:Select EffDate, CodeFrom HST as AWhere (row_number() over partition by Code order by EffDate desc) <= 3Order By Code , EffDate Desc- Lumbago |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-09-23 : 08:03:17
|
quote: Originally posted by Lumbago This method is somewhat old...here is a newer version compatible with 2005 and above:Select EffDate, CodeFrom HST as AWhere (row_number() over partition by Code order by EffDate desc) <= 3Order By Code , EffDate Desc- Lumbago
I doubt you can use windows functions directly in the where clauseMadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-09-23 : 08:05:32
|
quote: Originally posted by Vadivu I have a history table where records are stored as followsCode EffDate6789 9/23/20096789 9/20/20096789 8/21/20096789 7/14/20091353 9/23/20091353 9/20/20091353 8/14/20091353 8/7/2009how to write a query to get the recent 3 effdates for each record?I need an output as followsCode EffDate6789 9/23/20096789 9/20/20096789 8/21/20091353 9/23/20091353 9/20/20091353 8/14/2009Can anybody help me? thanks in advance.
Also referhttp://sqlblogcasts.com/blogs/madhivanan/archive/2008/09/12/return-top-n-rows.aspxMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|