tobias
tobias

Reputation: 2362

SQL query to sort table by sum

I have basically following table:

id (int) username (string) messsage (string) rating (int)

So the entries look like this:

1   thomas   "hello..."   3
2   Tina     "blabla"     2
3   thomas   "blub"       1
4   julia    "basgs"      3

...

I want retrieve the top 10 usernames with the most ratings for all their messages. So I want to sort the table that it looks

1. thomas 4
2. julia 3
3. Tina 2

Upvotes: 0

Views: 52

Answers (2)

MarcinJuraszek
MarcinJuraszek

Reputation: 125630

I'm not sue I understood your question right, but try that one:

SELECT
    username,
    SUM(rating)
FROM
    YourTable
GROUP BY
    username
ORDER BY
    SUM(rating) desc
LIMIT
    10

Upvotes: 2

Andomar
Andomar

Reputation: 238116

select  username
,       count(*)
from    YourTable
group by
        username
order by
        count(*) desc
limit   10

Upvotes: 0

Related Questions