I’m creating a video ranking website. Each video goes up against another video in a match up. Then the user moves forward to the next matchup.
The following query returns results sorted by win loss ratios.
SELECT v.id, v.wins, v.loses, v.wins / v.loses AS win_loss_ratio
FROM video
WHERE v.id NOT
IN (
SELECT h.competitorid
FROM video AS v, comphistory AS h
WHERE v.id = h.id
)
ORDER BY win_loss_ratio DESC
LIMIT 0 , 2
Results look like this:
id wins loses win_loss_ratio
73 7 2 3.5000
104 5 2 2.5000
I’m trying to figure out a way to pair videos similar to the swiss tournament method. http://en.wikipedia.org/wiki/Swiss-system_tournament
It works by pairing the top half with the bottom half. For instance, if there are eight videos returned my query I would like it arrange so that video number 1 is paired with number 5, number 2 is paired with number 6 and so on.
Does anybody have any suggestions to modify my query to arrange videos in that order?
**Update I was able to figure out how to perform query that selects one video with a high win/loss ratio and one video with a low win/loss ratio. The comphistory table keeps track of each video so they don’t go against each other twice.
SELECT videos. *
FROM (
SELECT videos.id, videos.wins/videos.loses as win_loss_ratio
FROM videos
WHERE
videos.videoid NOT IN (
SELECT h.id FROM videos AS video, comphistory as h WHERE video.id = h.id
)
ORDER BY win_loss_ratio ASC
LIMIT 0 , 1
) videos
UNION SELECT DISTINCT videos. *
FROM (
SELECT videos.id, videos.wins/videos.loses as win_loss_ratio
FROM videos
WHERE
videos.videoid NOT IN (
SELECT h.id FROM videos AS video, comphistory as h WHERE video.id = h.id
)
ORDER BY win_loss_ratio DESC
LIMIT 0 , 1
) videos
Thanks,
Tegan Snyder
Ranking the players by score, and pairing player 2N-1 with player 2N will be about as far as you can get in the SQL statement.
Outside the table, you will have to record who each player has played, and which round, so they don’t play each other again. For chess you will have to record if the player was black or white, so that you can reduce the number of people who have to play the same colour twice running and elliminate three-in-a-row scenarios. If you have an odd number of players, you will have to record who has sat out a round and been given a “bye”, so that they are not picked again.
I suggest you just read the data in to a table, and sort it such that you leave equal scores in a random order. Then (if required) remove a player at random to sit out this round. You will be left with a list of players, who might be paired up (1,2)(3,4)(5,6)..etc. however this simple method may fail becasue the pair have met before, or it would create a clash of the colour rules etc.. When this happens, you need a way to search for a possible solution, widening the search space as slowly as possible, until a solution is found.
For example, (continuing the above example), if 3 and 4 have already met, you might try (1,3)(2,4)(5,6)… and (1,2)(3,5)(4,6)… before trying other, more distant permutations such as (1,3)(2,5)(4,6)… A “best” solution will involve moving as few people from their “home” place as possible, and moving them as few steps as possible. If you can, search in a sequence so that the first solution found is acceptable.
The alternative is to generate all permutations, check that they solve the problem and score the solution found, so you can keep the best found so far. At the end you will have the best solition, but such a search will take much longer that searching smart and stopping at the first solution.