Advertisement
thunder_perfect

soccerGameSeries

Jul 27th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.15 KB | None | 0 0
  1. /*Please add ; after each select statement*/
  2. CREATE PROCEDURE soccerGameSeries()
  3. BEGIN
  4.     /*SELECT CASE
  5.         WHEN w.t1wins = w.t2wins
  6.             THEN CASE
  7.                 WHEN w.t1Diff > w.t2Diff AND t1AwayWins > t2AwayWins THEN 1
  8.                 WHEN w.t1Diff < w.t2Diff AND t1AwayWins < t2AwayWins THEN 2
  9.                 ELSE 0 END            
  10.         WHEN w.t1wins > w.t2wins
  11.             THEN 1 ELSE 2 END as winner */
  12. SELECT *
  13.     FROM
  14.     (
  15.         SELECT
  16.         SUM(CASE WHEN first_team_score > second_team_score OR (first_team_score = second_team_score AND match_host = 2) THEN 1 ELSE 0 end) as t1wins,
  17.         SUM(CASE WHEN first_team_score < second_team_score OR (first_team_score = second_team_score AND match_host = 1) THEN 1 ELSE 0 end) as t2wins,
  18.         MAX(first_team_score - second_team_score) as t1Diff,
  19.         MAX(second_team_score - first_team_score ) as t2Diff,
  20.         SUM(CASE WHEN first_team_score > second_team_score AND match_host = 2 then 1 ELSE 0 end) as t1AwayWins ,
  21.         SUM(CASE WHEN second_team_score > first_team_score AND match_host = 1 then 1 ELSE 0 end) as t2AwayWins
  22.         FROM scores
  23.     ) w;
  24. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement