Skip to content

Instantly share code, notes, and snippets.

@Tuan-T-Nguyen
Created March 7, 2018 06:32
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 Tuan-T-Nguyen/852b358f5539d477f33ac639ad60aca7 to your computer and use it in GitHub Desktop.
Save Tuan-T-Nguyen/852b358f5539d477f33ac639ad60aca7 to your computer and use it in GitHub Desktop.
SUM() based on a different condition to the SELECT
SELECT m.member_id, m.teamname,
Sum(Case When r.track_Id = '$chosentrack'
Then total_points Else 0 End) TotalChosenTrackPoints,
Sum(Case When r.track_Id < '$chosentrack'
Then total_points Else 0 End) TotalLessThanChosenTrackPoints,
total_points as last_race_points
FROM members m
Join members_leagues l
On l.member_id = m.member_id
Join member_results r
On r.member_id = m.member_id
Where l.league_id = '$chosenleague'
And l.start_race = '$chosentrack'
Group By m.member_id
Order By r.total_points Desc,
last_race_points Desc, m.TeamName Desc
/* https://stackoverflow.com/questions/7655219/sum-based-on-a-different-condition-to-the-select */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment