Reputation: 13
I have a MySQL table that looks something like this:
GOALS
------------------------
PLAYER | GOALS
------------------------
001 | 30
002 | 25
003 | 25
004 | 20
I want to have a rank-based scoring system that gives half-points for ties. In the above example, it should look like this:
GOALS
-----------------------------------
PLAYER | GOALS | SCORE
-----------------------------------
001 | 30 | 4
002 | 25 | 2.5
003 | 25 | 2.5
004 | 20 | 1
In case of a three-way tie:
GOALS
-----------------------------------
PLAYER | GOALS | SCORE
-----------------------------------
001 | 30 | 5
002 | 25 | 3
003 | 25 | 3
004 | 25 | 3
005 | 20 | 1
How would something like this be done in mySQL?
Thanks!
Upvotes: 0
Views: 119
Reputation: 781633
SELECT a.player, a.goals, (SUM(a.goals > b.goals) + 1 + SUM(a.goals >= b.goals))/2 AS score
FROM goals a
CROSS JOIN goals b
GROUP BY player
This works because SUM(a.goals > b.goals) + 1
is the minimum rank of all the tied players, while SUM(a.goals >= b.goals)
is the maximum rank. So this averages the two ends of the range to get the score.
Upvotes: 2