lassence.net

Field notes from the AI frontier

Get first or last value in a group

--- archive: codebook, 2020–2023 · topic: SQL ---

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:

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

If you want to get the last value, just use ORDER BY balance_updated_at DESC