Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 12890

How to delete records by age and keep only the 2 most recent for a set of key fields

$
0
0

I need to come up with a purge routine to keep only the two most recent records for each set of key fields.  For instance in this table:

CREATE TABLE [dbo].[Profile_Actuals](
[EffectiveDate] [datetime] NULL,
[Company] [varchar](10) NULL,

[Profile] [varchar](4) NULL,
[Value] [real] NULL
)
GO

For every company and profile combination I need to delete everything except the two most recent (by the EffectiveDate field).  Some of them may only have 1, but cannot have more than two.

 


Viewing all articles
Browse latest Browse all 12890

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>