For most SQL dialects, the easiest way to get the first (or last) value within groups is probably to add a row number, and then filter on 1
in the next step:
If you want to get the last value, just use ORDER BY balance_updated_at
DESC
For most SQL dialects, the easiest way to get the first (or last) value within groups is probably to add a row number, and then filter on 1
in the next step:
If you want to get the last value, just use ORDER BY balance_updated_at
DESC
WITH ranked AS (
SELECT
user_id,
balance,
ROW_NUMBER() OVER (
PARTITION BY user_id
ORDER BY balance_updated_at
) AS rnk
FROM users_balance
)
SELECT
user_id,
balance
FROM ranked
WHERE rnk = 1