Advertisement
Guest User

Untitled

a guest
May 24th, 2015
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.16 KB | None | 0 0
  1. CREATE VIEW v_matches_won_per_player AS
  2.     SELECT t_players.id, t_players.name,
  3.     COUNT(t_matches.id_winner) AS total_matches_won, t_matches.id_tournament
  4.     FROM t_players
  5.     JOIN t_matches
  6.     ON t_players.id = t_matches.id_winner
  7.     GROUP BY t_players.id, t_matches.id_tournament
  8.     ORDER BY total_matches_won DESC;
  9.  
  10.  
  11. CREATE VIEW v_matches_lost_per_player AS
  12.     SELECT t_players.id, t_players.name,
  13.     COUNT(t_matches.id_loser) AS total_matches_lost, t_matches.id_tournament
  14.     FROM t_players JOIN t_matches
  15.     ON t_players.id = t_matches.id_loser
  16.     GROUP BY t_players.id, t_matches.id_tournament
  17.     ORDER BY total_matches_lost DESC;
  18.  
  19.  
  20. CREATE VIEW v_total_stats AS
  21.     SELECT v_matches_won_per_player.id, v_matches_won_per_player.name,
  22.            v_matches_won_per_player.total_matches_won AS total_matches_won,
  23.            v_matches_lost_per_player.total_matches_lost AS total_matches_lost,
  24.            v_matches_won_per_player.total_matches_won + v_matches_lost_per_player.total_matches_lost AS total_matches_played
  25.     FROM v_matches_won_per_player, v_matches_lost_per_player
  26.     WHERE v_matches_won_per_player.id = v_matches_lost_per_player.id;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement