I have a query that gets top 5 accumulated score from the results table.
SELECT qm_1_users.userId,
qm_1_users.fname,
qm_1_users.lname,
SUM(qm_1_results.points) as topScore
FROM qm_1_results
INNER JOIN qm_1_users
ON qm_1_users.userid=qm_1_results.userid
GROUP BY
qm_1_users.userId
ORDER BY
topScore DESC
LIMIT 5;
I now need it to show the top 5 users who have the highest score achieved on any particular day. So if I played the quiz three times and achieved the following scores (Day 1 = 400, Day 2 = 600, Day 3 = 120), then my top score would be 600. This is where my SQL knowledge struggles.
quizid | userid | questionid | points
1 1 1 50
1 1 2 50
2 1 1 50
2 1 2 50
2 1 3 50
2 1 4 50
4 1 1 50
4 1 2 50
4 1 3 50
4 1 3 50
4 1 4 50
4 1 5 50
1 2 1 50
3 2 1 50
3 2 2 50
4 2 1 50
4 2 2 50
4 2 3 50
1 3 1 50
1 3 1 50
1 3 1 50
Player 1: Day 1 – 100 points, Day 2 – 200 points, Day 4 – 300 points Player 2: Day 1 – 50 points, Day 3 – 400 points, Day 4 – 150 points Player 3: Day 1 – 150 points
The top of the leaderboard would show:
Player 2 – 400 points in position 1 Player 1 – 300 points in position 2 Player 3 – 150 points in position 3
Can anyone help or point me in the right direction on how to do this?
You can group by user and day so you'll get the same result classified by day/person, then you can order by user_id and topScore (but not necessary).
By this way you'll get something like
If you add a filter per day at
wheresection you can select just this day top five with limit and order. Something likeWHERE qm_1_results.day = '2014-11-05'I suposse