I have the following table:
+-----------+--------+------+--------+----------+----------+---------+-------+
| player_id | europe | asia | africa | namerica | lamerica | oceania | total |
+-----------+--------+------+--------+----------+----------+---------+-------+
| 121693 | 1076 | 70 | NULL | NULL | NULL | NULL | 1146 |
| 121693 | 416 | 70 | NULL | NULL | NULL | NULL | 486 |
| 121693 | 40 | NULL | NULL | NULL | NULL | NULL | 40 |
| 118840 | 40 | NULL | NULL | NULL | NULL | NULL | 40 |
| 121693 | 40 | 70 | NULL | NULL | NULL | NULL | 110 |
| 118840 | 40 | 490 | NULL | NULL | NULL | NULL | 530 |
| 121691 | 96 | 0 | NULL | NULL | NULL | NULL | 96 |
| 121693 | 136 | 70 | NULL | NULL | NULL | NULL | 206 |
| 118840 | 40 | NULL | NULL | NULL | NULL | NULL | 40 |
| 121691 | 96 | NULL | NULL | NULL | NULL | NULL | 96 |
+-----------+--------+------+--------+----------+----------+---------+-------+
What I would like to get is something like:
+-----------+--------+------+--------+----------+----------+---------+-------+
| player_id | europe | asia | africa | namerica | lamerica | oceania | total |
+-----------+--------+------+--------+----------+----------+---------+-------+
| 121693 | 1076 | 70 | NULL | NULL | NULL | NULL | 1146 |
| 118840 | 40 | 490 | NULL | NULL | NULL | NULL | 530 |
| 121691 | 96 | 0 | NULL | NULL | NULL | NULL | 96 |
+-----------+--------+------+--------+----------+----------+---------+-------+
I have tried the following, but the results are not quite what I need.
SELECT player_id, europe, asia, africa, namerica,
lamerica, oceania, MAX(total) AS total
FROM tbl
WHERE total IS NOT NULL
GROUP BY player_id
ORDER BY total DESC
this is the result
+-----------+--------+------+--------+----------+----------+---------+-------+
| player_id | europe | asia | africa | namerica | lamerica | oceania | total |
+-----------+--------+------+--------+----------+----------+---------+-------+
| 121693 | 1076 | 70 | NULL | NULL | NULL | NULL | 1146 |
| 118840 | 40 | | NULL | NULL | NULL | NULL | 530 |
| 121691 | 96 | 0 | NULL | NULL | NULL | NULL | 96 |
+-----------+--------+------+--------+----------+----------+---------+-------+
Please note that I am using a MySQL view. Views in MySQL do not allow subqueries.
Edit:
Based on prior suggestions, I have tried this:
SELECT a.player_id, a.europe, a.asia, a.africa, a.lamerica, a.namerica, a.oceania, a.total
FROM tbl AS a
INNER JOIN (
SELECT player_id, MAX(total) AS total
FROM tbl
GROUP BY player_id) b
ON a.player_id = b.player_id AND a.total = b.total
But I get these results:
+-----------+--------+------+--------+----------+----------+---------+-------+
| player_id | europe | asia | africa | lamerica | namerica | oceania | total |
+-----------+--------+------+--------+----------+----------+---------+-------+
| 121693 | 1076 | 70 | NULL | NULL | NULL | NULL | 1146 |
| 118840 | 40 | 490 | NULL | NULL | NULL | NULL | 530 |
| 121691 | 96 | NULL | NULL | NULL | NULL | NULL | 96 |
| 121691 | 96 | 0 | NULL | NULL | NULL | NULL | 96 |
+-----------+--------+------+--------+----------+----------+---------+-------+
It also returns a tie. How is it possible to avoid this?