Skip to content

Instantly share code, notes, and snippets.

@shadefinale
Created August 18, 2015 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shadefinale/fa1543ddcd0e423986f8 to your computer and use it in GitHub Desktop.
Save shadefinale/fa1543ddcd0e423986f8 to your computer and use it in GitHub Desktop.
all_play_by_season_losses
WITH weekly_points_table (team_id, week_id, max_points)
AS
(
SELECT teams.id, weeks.id as week_id, SUM(player_scores.points) as max_points
FROM player_scores
INNER JOIN weeks ON weeks.id = player_scores.week_id
INNER JOIN teams ON teams.id = player_scores.team_id
WHERE (team_id IN (?) AND weeks.year = ?)
GROUP BY teams.id, weeks.id
)
SELECT COUNT(*) as season_losses
FROM weekly_points_table AS x
WHERE x.max_points > (SELECT max_points
FROM weekly_points_table
WHERE team_id = ? AND x.week_id = weekly_points_table.week_id
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment