user1372896
user1372896

Reputation: 534

Get top 5 total results

I've attempted to grab the top 10 donations from our database. Each steamid can donate multiple times so multiple donations need to be added together to get the total. An example table is:

steamid             amount      email                   date
76561197991519598   25          [email protected]     1445107360
76561198129490626   10          [email protected]     1445106920
76561197994977992   5           [email protected]     1445107724
76561197991519598   25          [email protected]     1445107519
76561197994977992   50          [email protected]     1445107047

The result should be:

76561197994977992 = 55
76561197991519598 = 50
76561198129490626 = 10

(ordered from the most to the least).

I've tested something myself and gotten odd results here is what I tried:

SELECT st.*
FROM donations st
WHERE st.amount = 
    (SELECT SUM(t.amount) 
    FROM donations t 
    WHERE t.steamid = st.steamid) 
GROUP BY st.steamid
ORDER BY st.amount

Upvotes: 0

Views: 34

Answers (1)

Pablo Digiani
Pablo Digiani

Reputation: 602

Try this:

select steamid, sum(amount) as total from donations
group by steamid
order by 2 desc
limit 5

Upvotes: 1

Related Questions