I am trying to find a way to rank people's scores through multiple categories. The table contains a list of every player along with the event they participated in, and their score.
The query needs to assign a rank to each player based on score within each event. (The Ranks assigned in Event 1 and Event 2 should be completely separate.)
If the player is missing from an event, they should be assigned a Rank of (Total players in that category + 1)
I can do a basic ranking over 1 category. That's simple. But I don't even have a clue how to do this.
Data in table
+--------+-------+---------+
| Name | Score | Event # |
+--------+-------+---------+
| Kevin | 73 | Event 1 |
| George | 69 | Event 1 |
| Henry | 70 | Event 1 |
| | | |
| George | 45 | Event 2 |
| Kevin | 65 | Event 2 |
| Henry | 65 | Event 2 |
| Daniel | 50 | Event 2 |
+--------+-------+---------+
Expected return
+---------+--------+------+
| Event # | Name | Rank |
+---------+--------+------+
| Event 1 | Kevin | 1 |
| Event 1 | Henry | 2 |
| Event 1 | George | 3 |
| Event 1 | Daniel | 4 |
| Event 2 | Kevin | 1 |
| Event 2 | Henry | 1 |
| Event 2 | Daniel | 3 |
| Event 2 | George | 4 |
+---------+--------+------+
Note the ranking is NOT dense! I am on MySql 5.7.
In actuality I have over 50 events and hundreds of people.
This is what I have for ranking 1 event
SELECT
IF( SCORE =@_last_rank,@curRank:=@curRank,@curRank:=@_sequence) AS RANK,
NAME,
EVENT,
@_sequence:=@_sequence+1,@_last_rank:= SCORE
FROM (SELECT * FROM database WHERE EVENT = $event) p,
(SELECT @curRank := 1, @_sequence:=1, @_last_rank:=0) r
ORDER BY RANK, NAME