I have a table account with the fallowing structure:
| agg_type | agg_id | sequence | payload | is_snapshot | timestamp |
| "account" | "agg_1" | 1 | "..." | false | ... |
| "account" | "agg_1" | 2 | "..." | true | ... |
| "account" | "agg_1" | 3 | "..." | false | ... |
| "account" | "agg_1" | 4 | "..." | false | ... |
| "account" | "agg_1" | 5 | "..." | false | ... |
| "account" | "agg_1" | 6 | "..." | false | ... |
| "account" | "agg_1" | 7 | "..." | true | ... |
| "account" | "agg_1" | 8 | "..." | false | ... |
I need to write a query that will retrieve all rows from this table from the latest snapshot onward of an specific aggregate. For instance, in the case of this table the query would return the last two rows (sequences 7 and 8).
I think that the query would go something like
SELECT * FROM account
WHERE
agg_type='account'
AND agg_id='agg_1'
ORDER BY sequence ASC
LIMIT (???);
It's the (???) part that I'm not quite sure on how to implement.
Obs:
- I'm using Postgres if it is of any help.
- The (agg_type, agg_id, sequence) combination is a primary key.