Prune Old Records

Prune Old Records

 

  

I have the following bit of sql:

DELETE FROM a
FROM UserLoginLog a JOIN
(SELECT UserName, max(LoginDateTime) Max_Date FROM UserLoginLog
GROUP BY UserName
HAVING COUNT(*) > 1) b
ON a.UserName = b.UserName and a.LoginDateTime < b.Max_Date

And it deletes all records with multiple usernames except the newest one.
Does anyone know how I can alter this code to make it such that it deletes
all records with multiple usernames except the newest FIVE entries? X
entries?

Thanks,

Alvin

MS Sql Server LazyDBA home page