I have a table of players and I'm trying to build an SQL query to pair them up to assign matches for a particular round. Each player has a score, and I want to pair players up that have the similar scores. I don't want any player to appear in more than one pair.
To start, I created the following view:
CREATE VIEW all_possible_pairs AS SELECT p1.id AS player1, p2.id AS player2 FROM players as p1, players as p2 WHERE p1.id < p2.id ORDER BY ABS(p1.score - p2.score) all_possible_pairs gets all of the possible pairs of players and avoids matching players with themselves and including the same pair twice (e.g. as a,b b,a). The data is ordered the way I want such that pairs that appear first are preferred to pairs that appear later (since they have closer scores).
I want to select the rows from all_possible_pairs where a player first appears. In the resulting table, each players should appear only once across the two columns (e.g. if a player first appears in player2, they should not appear in player1 or player2 in any subsequent rows).
So, for example, suppose we have players a, b, c, d and all_possible_players looks like this:
player1, player2 a b a c a d b c b d c d I want to select from this view so that I get the following:
player1, player2 a b c d I've been banging my head against the wall for a while now trying various SELECT DISTINCT clauses but I can't seem to get it right. For example, a SELECT DISTINCT on player1 in the example above would include b,c, which is not what I want since it means b and c are in the table twice.
VARCHAR?