I have a table as:
table t1
id int
room text
record_date timestamp
How can I delete some records in t1 table, except the newest 100 records of a room(record_date is used to save the timestamp to compare with)?
With mysql I did the next and it works:
DELETE p FROM t1 p
LEFT JOIN
(SELECT id,room
FROM t1
ORDER BY record_date DESC
LIMIT 101) p2 on p2.id=p.id and p2.room=p.room
WHERE p2.id IS NULL and p.room='myroom'
But postgres doesn´t works with it and results on error.
3
Answers
Try the following :
Try this adapted from this answer:
You could try the following command:
The reason of using ‘NOT EXISTS’ will ensure that selected records are older than the 100th newest record in the room.