Resolving ties in MySQL rankings -


i have mysql table looks this:

goals ------------------------    player   |   goals ------------------------      001    |    30      002    |    25      003    |    25      004    |    20 

i want have rank-based scoring system gives half-points ties. in above example, should this:

goals -----------------------------------    player   |   goals   |   score -----------------------------------      001    |    30     |    4      002    |    25     |    2.5      003    |    25     |    2.5      004    |    20     |    1 

in case of three-way tie:

goals -----------------------------------    player   |   goals   |   score -----------------------------------      001    |    30     |    5      002    |    25     |    3      003    |    25     |    3      004    |    25     |    3      005    |    20     |    1 

how done in mysql?

thanks!

select a.player, a.goals, (sum(a.goals > b.goals) + 1 + sum(a.goals >= b.goals))/2 score goals cross join goals b group player 

demo

this works because sum(a.goals > b.goals) + 1 minimum rank of tied players, while sum(a.goals >= b.goals) maximum rank. averages 2 ends of range score.


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -