I have two tables
Players id Player 1 Jack 2 Anna 3 Sam Scores id player_id score 1 1 500 2 1 200 3 2 300 4 2 200 5 3 750
I would like the output to look like
1 Sam 750 2 Jack 500 3 Anna 300
Currently my code looks like
SELECT ROW_NUMBER() OVER (ORDER BY S.score DESC, P.name), P.name, S.score FROM Players P, Scores S WHERE P.id = S.player_id;
but my output is
1 Sam 750 2 Jack 500 3 Anna 300 4 Anna 200 5 Jack 200
I’m not sure how to get distinct player names, my attempts at using DISTINCT have not been working out. I’m just learning so apologies if this is something really obvious, i’ve tried to look for answers without success.
Advertisement
Answer
SELECT ROW_NUMBER() OVER (ORDER BY x.score DESC, x.name), x.name, x.score FROM ( SELECT P.Name, Max(S.Score) as score FROM Players P, Scores S WHERE P.id = S.player_id GROUP BY P.Name ) x ORDER BY x.score DESC, x.Name
First get the highest score per user, than rank the stuff.