I am relatively new to SQL and have got a standings page to work ok if I only look at the home team... here's my sample table
ScheduleTable
DIVISION HOMETEAM VISITORTEAM HOMESCORE VISITORSCORE
A leafs blues 1 1
B wings leafs 0 3
A stars leafs 0 5
I am trying to output to this
Division Team Games Played, Wins, Losses, Ties, Goals For, Goals Against, Points sorted by Points
(Points = 2 for a win and 1 for a tie)
I can't get the points to calc yet either
here is my sql statement:
I get everything to work if I only look at the home team except for the points. What I need hlp with is getting the points to calculate and include the results for the visitors as well so that I end up with table that looks like this:
Division Team GP W L T GF GA PTS
A stars 1 1 0 0 5 0 2
A blues 1 0 0 1 1 1 1
A leafs 2 0 1 1 1 6 1
B leafs 1 1 0 0 3 0 2
B wings 1 0 1 0 0 3 0
ScheduleTable
DIVISION HOMETEAM VISITORTEAM HOMESCORE VISITORSCORE
A leafs blues 1 1
B wings leafs 0 3
A stars leafs 0 5
I am trying to output to this
Division Team Games Played, Wins, Losses, Ties, Goals For, Goals Against, Points sorted by Points
(Points = 2 for a win and 1 for a tie)
I can't get the points to calc yet either
here is my sql statement:
Code:
SELECT division, hometeam, visitorteam, sum( homescore ) as total_goals_for, sum( visitorscore ) as total_goals_against, count( homescore ) as games_played, sum( CASE WHEN homescore > visitorscore THEN 1 ELSE 0 END ) as wins, sum( CASE WHEN homescore < visitorscore THEN 1 ELSE 0 END ) as loss, sum( CASE WHEN homescore = visitorscore THEN 1 ELSE 0 END ) as ties FROM scheduletable GROUP BY division, hometeam
I get everything to work if I only look at the home team except for the points. What I need hlp with is getting the points to calculate and include the results for the visitors as well so that I end up with table that looks like this:
Division Team GP W L T GF GA PTS
A stars 1 1 0 0 5 0 2
A blues 1 0 0 1 1 1 1
A leafs 2 0 1 1 1 6 1
B leafs 1 1 0 0 3 0 2
B wings 1 0 1 0 0 3 0