Group By and get top N in Simple SQL Group By and get top N in Simple SQL sqlite sqlite

Group By and get top N in Simple SQL


I managed to do it with using only simple function with self-join.

Now you can just update N with your preferred value, for my case top 3, it would be LIMIT 3.

SELECT receiver-name ,(    SELECT SUM(amount) as sum_amount    FROM (        SELECT amount        FROM bank as b2           WHERE b2.receiver-name = b.receiver-name        ORDER BY b2.amount DESC        LIMIT 3     )) as sum_amountFROM bank as bGROUP BY receiver-name