Aggregate SQL Function to grab only the first from each group

Rather than grouping, go about it like this…

select
    *

from account a

join (
    select 
        account_id, 
        row_number() over (order by account_id, id) - 
            rank() over (order by account_id) as row_num from user
     ) first on first.account_id = a.id and first.row_num = 0

Leave a Comment