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

mysql - FireDac error 314 - but DLLs are in program directory -

git - How to list all releases of public repository with GitHub API V3 -

c++ - Getting C2512 "no default constructor" for `ClassA` error on the first parentheses of constructor for `ClassB`? -