sqlite equivalent of row_number() over ( partition by …?

I know this question is old, but the following SQLite statement will do what Rory was originally asking for in one statement – Delete all records for a given UserId that are not the 10 most recent records for that UserId (based on SomeDate).

DELETE FROM data
WHERE AnId IN (SELECT AnId
               FROM data AS d
               WHERE d.UserId = data.UserId
               ORDER BY SomeDate DESC
               LIMIT -1 OFFSET 10)

Leave a Comment