Skip to content
Advertisement

Count Most donator SQL

I wanted to know how would i count most donated number for a person and how to make list from most donator to the lowest.

Database looks like this :

 - Donated Amount - Payer Email
 - 10   123456@hotmail.com
 - 5    125643@hotmail.com
 - 5    123456o@gmail.com
 - 20   6653211@live.de
 - 5    1256431@live.de
 - 10   6558714@gmail.com

Note : If anyone would guide me on how the table is made on stackoverflow, thank you.

Advertisement

Answer

The easiest is to group by the email, sum the amount to get a total, and order by descending total.

SELECT 
 `Payer Email`, 
 SUM(`Donated Amount`) AS DonatedAmount
-- , COUNT(*) AS TimesDonated
-- , MAX(`Donated Amount`) AS BiggestDonation
FROM YourDonationsTable
GROUP BY `Payer Email`
ORDER BY DonatedAmount DESC

And if you want only the top 3 of those, add a LIMIT 3 at the end of the SQL.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement