I have a table called ratings that looks like this
Rating | Picid | User 8 12 6 7 12 6 9 15 7 5 16 7 9 17 8 10 2 8 7 18 3 10 22 12
I want to group the ratings based on the picid, but only choose the highest rated picid per user. I have the query like this already:
SELECT *, AVG(rating) AS total FROM ratings
GROUP BY picid ORDER by total DESC
Right now the query would output the information like this:
Rating | Picid | User 7.5 12 6 9 15 7 5 16 7 9 17 8 10 2 8 7 18 3 10 22 12
So it successfully groups the ratings based on the picid, but I want only the ONE highest per user. I want it to output information like this:
Rating | Picid | User 7.5 12 6 9 15 7 10 2 8 7 18 3 10 22 12
See how it only allows the highest rated picid per user?
How can I modify my query to do this?
Let me know if you need more clarification on what I’m asking.
You first must calculate the average ratings for each
(Picid,User)combination, then find the group-wise maximum over that materialised result:See it on sqlfiddle.