I am writing an MVP system based on votes from users. The table will look like
vote | game | year --------- ------ ------ player 1 | 2 | 2015 player 1 | 2 | 2015 player 2 | 2 | 2015 player 2 | 2 | 2015 player 2 | 2 | 2015 player 3 | 2 | 2015 player 1 | 2 | 2015 player 3 | 2 | 2015 player 4 | 2 | 2015
An entry here equates to a vote. So out of this we would group and get a count of
Player 1 = 3 Player 2 = 3 Player 3 = 2 Player 4 = 1
The MVP system is a "3-2-1" score system. So points are awarded to the players in reverse order. So it should only apply winning scores to the top 3 positions. i.e.
1st gets 3 points 2nd gets 2 points 3rd gets 1 point
This gets really complicated when there is a tie but top 3 should get MVP points.
In the case above the point spread would be
Player 1 = 2.5 points
Player 2 = 2.5 points
Player 3 = 1 point
This is worked out because the top 2 players are effectively 1st and 2nd, there is no second so the next person gets 1 point for placing 3rd.
Effectively (points = points_available / players_in_rank)
however that equation is wrong. It gets extra complicated if we have a tie for 3rd, where you could possibly end up with lots of people in 3rd spot and therefore they get 1 point divided by them.
It is really complicated when you have ties in 1st or second. I have worked around this poorly in PHP code but would like to be able to do this in SQL and do away with PHP.
Just wondering if anyone has any simple methods for this?
These scores points are then later added to an overall standings table
Regards
Dan
Best Answer
Sample data:
Query:
Result:
Explanation:
This
is simple enough, I hope :)
This
is just a subquery to initialize some variables.
Here
in the first row
@prevNoVotes
holds the value of the previous row (or the value from the initialization subquery).@rc1
,@rc2
and@rc3
are just rank counters. They count for each rank, how many people are on the same rank.In the last row, the value of the current row is assigned to the variable. When the next row is processed, the variable will hold the value of the previous row in the first line of this part of the code.
And finally in the most outer part of the query we calculate the points distribution.
Feel free to ask any questions :)
UPDATE:
Added this table:
Added more logic :)