I saw there are numerous question similar, but I have depleted every solution I found. None of them work as expected for me.
My scenario is as following:
- Table with user's score (other data stripped, not relevant):
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | MUL | NULL | |
| score | int(11) | NO | | NULL | |
| rank | int(11) | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
- Table with user's (other data stripped, not relevant):
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| group_id | int(11) | NO | MUL | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
- Table with groups (other data stripped, not relevant):
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
Requirement is:
- Calculate rank based on score for 100 users regardless of group, and increment rank accordingly.
- Calculate rank based on score for 100 users based on group (each set of result for each group should have it's own rank), and increment rank accordingly.
Desired result is:
+----+------------+----------+------+
| id | user_id | score | rank |
+----+------------+----------+------+
| 2 | 29 | 10 | 1 |
| 5 | 32 | 3 | 2 |
| 6 | 33 | 2 | 3 |
| 7 | 34 | 0 | 4 |
| 9 | 39 | 0 | 5 |
| 11 | 41 | 0 | 6 |
| 15 | 47 | 0 | 7 |
| 18 | 51 | 0 | 8 |
+----+------------+----------+------+
Basically the users should be ranked by score, and the rank should continue until 100 even if they have 0.
Solutions tested:
- Calculate rank of the users based on their max score using mysql
- Rank users in mysql by their total points across multiple rows
- Rank users in mysql by their points
- How can I calculate rank based on highest Score and lowest Minutes in MySql Server
And more, but the result is the same:
+----+------------+----------+------+
| id | user_id | score | rank |
+----+------------+----------+------+
| 2 | 29 | 10 | 1 |
| 5 | 32 | 3 | 2 |
| 6 | 33 | 2 | 3 |
| 7 | 34 | 0 | 4 |
| 9 | 39 | 0 | 4 | <-- NOT OK (should be 5)
| 11 | 41 | 0 | 4 | <-- NOT OK (should be 6)
+----+------------+----------+------+
Notice that some users with score 0 get a rank... "4" and it stops there. I tried to fix this quite a long time but no success.
Haven't even got to the point to calculate per group properly.
Latest query used:
SELECT
T1.id,
T1.`user_id`,
T1.`score`,
T2.rank
FROM
`user_scores` T1
LEFT JOIN (
SELECT
`score`,
(@v_id := @v_Id + 1) AS rank
FROM
(
SELECT DISTINCT
`score`
FROM
`user_scores`
ORDER BY
`score` DESC
) t,
(SELECT @v_id := 0) r
) T2 ON T1.`score` = T2.`score`
ORDER BY
`score` DESC
LIMIT 100
I hope my question is clear enough and that someone can provide me a solution/explanation.
Thanks