Search code examples
sql-server-ce

Sql Compact 3.5: Limit number of rows in column


I have a table with rows of the following form: Timestamp Data: I would like to keep only newest N rows in table, and delete everything rest.

Is there a way to specify deletion of all except N newest rows?


Solution

  • delete from table 
    where id not in (
        select top 30 id from table
        order by timestampcolumn desc
    )
    

    Here N = 30. You can replace the number 30 by any number you want to retain.