I have this table :
| Column | Type |
+---------------+--------------------------------+
| id | integer |
| recipient_id | integer |
| is_read | boolean |
| updated_at | timestamp(0) without time zone |
I have to delete items from this table with this specific rule:
- for each
recipient_id, we keep the 5 last read items, and we delete the old read one.
I tried to bend my mind with RECURSIVE WITH statements but failed miserably. I’ve implemented my solution programmatically but I wanted to know if there was a decent pure SQL solution.
A
JOINis usually faster than an IN expression, especially with larger numbers of items.And use
row_number(), notrank()!