php - Remove all entries except last 15 from database for each UserID
I have a SQL server database and I would like to delete every row except the last 15, but, I need this to be per 'UserID'.
My database contains entries for a number of different users, where their ID is column 'UserID', so I want each user to have 15 entries in the database and the old ones to be removed.
What would the SQL be for this?
Here is an example effort from myself:
$sql = "DELETE FROM SocialSenseTracking WHERE UserID NOT IN (SELECT TOP 15 UserID='$user' FROM SocialSenseTracking ORDER BY UserID DESC)";
You can use a CTE and
You don't specify the column used to determine the most recent records, so I just invented