CREATE TABLE Player (
id BIGINT NOT NULL PRIMARY KEY,
xp BIGINT NOT NULL
);
INSERT INTO Player (id, xp) VALUES (1, 100);
INSERT INTO Player (id, xp) VALUES (2, 150);
INSERT INTO Player (id, xp) VALUES (3, 250);
INSERT INTO Player (id, xp) VALUES (4, 125);
INSERT INTO Player (id, xp) VALUES (5, 500);
This query:
SELECT id, xp, RANK() over (ORDER BY xp DESC) 'rank' FROM Player;
Produces these results:
+----+-----+------+
| id | xp | rank |
+----+-----+------+
| 5 | 500 | 1 |
| 3 | 250 | 2 |
| 2 | 150 | 3 |
| 4 | 125 | 4 |
| 1 | 100 | 5 |
+----+-----+------+
But when I add WHERE id = '.....', the rank comes back as 1
.
I know that this is because it's now ranking over just one row, so it'll always be 1
.
But how would I select the correct rank just for a single row? Is the RANK() function still appropriate for this?
I don't mind about duplicate scores.
Asked by Lucien
(123 rep)
Oct 22, 2022, 10:53 AM
Last activity: Oct 23, 2022, 01:02 PM
Last activity: Oct 23, 2022, 01:02 PM