Sample code in PostgreSQL

[code] SELECT account, MAX(ARRAY[id, (data->‘credit’)::int]) FILTER (WHERE type = ‘credit_set’)) FROM event GROUP BY account; [/code]

ref: https://hakibenita.com/sql-group-by-first-last-value