Hi,
I have a problem which is driving me nuts and I just can't see how to get the answer. I would really appreciate it if somebody could even point me in the right direction.
I have a simple db that store details of a game. A game consists of rounds and the winner has the highest total of all rounds. I am trying to write a query that adds up the round scores for each player for each game and returns the highest score and player id of each game. It sounds so easy but I'm struggling to do the grouping inside a subqueary to get only the max score for each game.
Here is a simplified version of the table
Code:
+----+------+--------+-------+-------+
| id | game | player | round | score |
+----+------+--------+-------+-------+
| 1 | 1 | 1 | 1 | 3 |
| 2 | 1 | 2 | 1 | 3 |
| 3 | 1 | 1 | 2 | 1 |
| 4 | 1 | 2 | 2 | 2 |
| 5 | 1 | 3 | 1 | 5 |
| 6 | 1 | 3 | 2 | 1 |
| 7 | 2 | 1 | 1 | 8 |
| 8 | 2 | 2 | 1 | 4 |
| 9 | 2 | 3 | 1 | 1 |
| 10 | 2 | 1 | 2 | 1 |
| 11 | 2 | 2 | 2 | 4 |
| 12 | 2 | 3 | 2 | 6 |
+----+------+--------+-------+-------+
If I run
select game, player, sum(score) from scorecard group by game, player
I get
Code:
+------+--------+------------+
| game | player | sum(score) |
+------+--------+------------+
| 1 | 1 | 4 |
| 1 | 2 | 5 |
| 1 | 3 | 6 |
| 2 | 1 | 9 |
| 2 | 2 | 8 |
| 2 | 3 | 7 |
+------+--------+------------+
And now all I need to do is limit the result to the higest scores in each game which I think I need to do by using a sub-query and max but I always get the wrong answer.
Can anybody point out what I'm missing or how I could re-structure the table to make this work.
Many thanks if you can help at all.
Robin