2015-08-19 26 views

回答

1

試試這個:

DELETE 
    FROM Tablex 
    INNER JOIN (SELECT max(InsertDt) LastDate, id FROM Tablex GROUP by id) LastDates ON LastDates.id = Tablex.id 
    WHERE InsertDt < dateadd(m,-3,LastDates.LastDate) 
+0

DELETE Tablex FROM Tablex INNER JOIN(SELECT max(InsertDt)LastDate,id FROM Tablex GROUP by id)LastDates ON LastDates.id = Tablex .id WHERE InsertDt iosDev

1

的問題是不明確的,但如果你的意思是你想刪除的記錄比舊3個月你應該這樣做:

DELETE from Tablex 
    WHERE datediff(m,InsertDt , getdate())> 3 
+0

我想刪除表中每個id的最近3個月的記錄,而不是單個id – iosDev