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

前端 未结 5 1588
说谎
说谎 2020-11-30 08:32

I\'d like to know if it\'s possible to do the following using a single sqlite statement:

My table looks something like this:

|AnId|UserId|SomeDate|So         


        
5条回答
  •  一生所求
    2020-11-30 09:27

    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)
    

提交回复
热议问题