I'm using PostgreSQL and I would like to get a list of all the player's rank from one table.
For example, I have this table "Scores" : (the combination user_id - battlefield_id is unique in this table)
+----------+----------------+-------+
| user_id | battlefield_id | score |
+==========+================+=======+
| Legolas | Helm's Deep | 42 |
+----------+----------------+-------+
| Legolas | Pelennor | 100 |
+----------+----------------+-------+
| Gimli | Helm's Deep | 43 |
+----------+----------------+-------+
| Gimli | Pelennor | 120 |
+----------+----------------+-------+
| Aragorn | Helm's Deep | 50 |
+----------+----------------+-------+
| Aragorn | Pelennor | 10 |
+----------+----------------+-------+
And I would like a request to compute all the ranks of Aragorn on any of the battlefields and return this kind of result :
+------+----------------+
| rank | battlefield_id |
+======+================+
| 1 | Helm's Deep |
+------+----------------+
| 3 | Pelennor |
+------+----------------+
I currently have this request who works well but I have to specify both the user and the battlefield :
SELECT count(*)+1 AS rank FROM scores WHERE score <
(
SELECT score FROM scores WHERE user_id = 'Aragorn' AND battlefield_id = "Pelennor"
)
AND battlefield_id = "Pelennor"
But I have no idea how to adapt that to get the rank on any battlefield. I don't have to manage ties.