sql server 2008 - Delete all records from a table except latest N months records (sorted by id) -


can please me in solving this. need keep latest 3 months records in table.

table structure:

enter image description here

after purge table should

enter image description here

try this:

delete     tablex      inner join (select max(insertdt ) lastdate, id tablex  group id) lastdates on lastdates.id = tablex.id     insertdt < dateadd(m,-3,lastdates.lastdate) 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -