0

In a database that stores tournaments I have this relational structure to store matches and their games:

EER Diagram

When a match is created, a match row and multiple game rows are inserted corresponding to the number of games in the match. Match_left and match_right players rows are also inserted then to relate the two players in a match to the match row. When a player is recorded as having won a game, a row related to the corresponding game is inserted into the game_winners table. Is there a way to write a query to derive who won matches from won games? Perhaps logically this should be a view so that I can join match table rows to it at any time easily?

Here is some sample data

I would expect Fred to be returned as the winner of match_ID 1 because he won 2/3 games. I would expect the winner of match_ID 2 to be null because neither player has won the necessary number of games to win (2/3).

Desuse
  • 15
  • 3

2 Answers2

0

I'm assuming here that the winner of a match is the player who's won the most games.

Ranking the players within a match is straightforward.

SELECT player_id, COUNT(*) AS game_count
  FROM game_winners
  WHERE match_id = ?
  GROUP BY player_id
  ORDER BY COUNT(*) DESC

Finding the winners of more than one match at once is a little tricker.

SELECT match_id, player_id, COUNT(*) AS game_count
  FROM game_winners
  GROUP BY match_id, player_id

Let's call the above player_wins

SELECT match_id, MAX(game_count) AS match_winner
  FROM player_wins
  GROUP BY match_id

This gives you the winning score for each match.

SELECT match_id, player_id
  FROM player_wins
  INNER JOIN (
    SEELCT match_id, MAX(game_count) AS match_winner
    FROM player_wins
    GROUP BY match_id
  ) AS winning_score
  ON player_wins.match_id = winning_score.match_id
  AND player_wins.game_count = winning_score.match_winner

This gives you the winning player for each match.

Neil
  • 54,642
  • 8
  • 60
  • 72
  • Does this account for the fact that not every game for a match might have a winner yet if the match is still in progress? A kind of match winner field would be NULL in that case. Or that if a player wins the first 2 games in a best of 3, the last game would not be played and never get a winner, because that player won the match. – Desuse Aug 05 '11 at 23:17
  • I haven't looked at your update yet, but it may well invalidate my answer. – Neil Aug 06 '11 at 22:32
0

Let's see if I've understood your sample data correctly.

SELECT match_id, round_id, COUNT(*) AS game_count
  FROM games
  GROUP BY match_id, round_id

This gives you the number of games in each match. The match is a best of format, so you have to win more than half of the number of games to win the match.

SELECT match_id, round_id, player_id, COUNT(*) AS win_count
  FROM game_winners
  GROUP BY match_id, round_id

This gives you the number of games won by each player in each match.

SELECT match_id, round_id, player_id
  FROM game_winners
  GROUP BY match_id, round_id
  HAVING COUNT(*) * 2 > (
    SELECT COUNT(*)
      FROM games
      WHERE games.match_id = game_winners.match_id
      AND games.round_id = game_winners.round_id
  )

So one option is to add a check that the player has won more than half the number of games in the match via a subquery.

SELECT games_won.match_id, games_won.round_id, games_won.player_id
  FROM (
    SELECT match_id, round_id, player_id, COUNT(*) AS win_count
      FROM game_winners
      GROUP BY match_id, round_id
    ) AS games_won
  INNER JOIN (
    SELECT match_id, round_id, COUNT(*) AS game_count
      FROM games
      GROUP BY match_id, round_id
    ) AS all_games
  ON games_won.match_id = game_count.match_id
  AND games_won.round_id = game_count.round_id
  WHERE games_won.win_count * 2 > all_games.game_count

Or if you prefer you can do it using a join.

Neil
  • 54,642
  • 8
  • 60
  • 72
  • This appears to work, but I had to split the query into 3 views because of the subqueries. I should probably do some benchmarks to see what kind of speed I get from joining 3 views when I have a large amount of records. – Desuse Aug 09 '11 at 16:56