I'm trying to calculate League Standings from a table of Matches.
+----------------------------------+
| Matches |
+----------------------------------+
| id |
| league_id (FK League) |
| season_id (FK Season) |
| home_team_id (FK Team) |
| away_team_id (FK Team) |
| home_score |
| away_score |
| confirmed |
+----------------------------------+
I can correctly calculate the Home League Standings using this query:
SELECT team.name, home_team_id AS team_id,
COUNT(*) AS played,
SUM((CASE WHEN home_score > away_score THEN 1 ELSE 0 END)) AS won,
SUM((CASE WHEN away_score > home_score THEN 1 ELSE 0 END)) AS lost,
SUM((CASE WHEN home_score = away_score THEN 1 ELSE 0 END)) AS drawn,
SUM(home_score) AS goalsFor,
SUM(away_score) AS goalsAgainst,
SUM(home_score - away_score) AS goalDifference,
SUM((CASE WHEN home_score > away_score THEN 3 WHEN home_score = away_score THEN 1 ELSE 0 END)) AS points
FROM matches
INNER JOIN team ON matches.home_team_id = team.id
WHERE league_id = 94
AND season_id = 82
AND confirmed IS NOT NULL
GROUP BY home_team_id
ORDER BY POINTS DESC;
And Away League Standigns using this query:
SELECT team.name, away_team_id AS team_id,
COUNT(*) AS played,
SUM((CASE WHEN away_score > home_score THEN 1 ELSE 0 END)) AS won,
SUM((CASE WHEN home_score > away_score THEN 1 ELSE 0 END)) AS lost,
SUM((CASE WHEN home_score = away_score THEN 1 ELSE 0 END)) as drawn,
SUM(away_score) AS goalsFor,
SUM(home_score) AS goalsAgainst,
SUM(away_score - home_score) AS goalDifference,
SUM((CASE WHEN away_score > home_score THEN 3 WHEN away_score = home_score THEN 1 ELSE 0 END)) AS points
FROM matches
INNER JOIN team ON matches.away_team_id = team.id
WHERE league_id = 94
AND season_id = 82
AND confirmed IS NOT NULL
GROUP BY away_team_id
ORDER BY points DESC;
But combining these two queries using UNION ALL I'm not getting correct result
SELECT * FROM
(
SELECT team.name, home_team_id AS team_id,
COUNT(*) AS played,
SUM((CASE WHEN home_score > away_score THEN 1 ELSE 0 END)) AS won,
SUM((CASE WHEN away_score > home_score THEN 1 ELSE 0 END)) AS lost,
SUM((CASE WHEN home_score = away_score THEN 1 ELSE 0 END)) AS drawn,
SUM(home_score) AS goalsFor,
SUM(away_score) AS goalsAgainst,
SUM(home_score - away_score) AS goalDifference,
SUM((CASE WHEN home_score > away_score THEN 3 WHEN home_score = away_score THEN 1 ELSE 0 END)) AS points
FROM matches
INNER JOIN team ON matches.home_team_id = team.id
WHERE league_id = 94
AND season_id = 82
AND confirmed IS NOT NULL
GROUP BY home_team_id
UNION
SELECT team.name, away_team_id AS team_id,
COUNT(*) AS played,
SUM((CASE WHEN away_score > home_score THEN 1 ELSE 0 END)) AS won,
SUM((CASE WHEN home_score > away_score THEN 1 ELSE 0 END)) AS lost,
SUM((CASE WHEN home_score = away_score THEN 1 ELSE 0 END)) as drawn,
SUM(away_score) AS goalsFor,
SUM(home_score) AS goalsAgainst,
SUM(away_score - home_score) AS goalDifference,
SUM((CASE WHEN away_score > home_score THEN 3 WHEN away_score = home_score THEN 1 ELSE 0 END)) AS points
FROM matches
INNER JOIN team ON matches.away_team_id = team.id
WHERE league_id = 94
AND season_id = 82
AND confirmed IS NOT NULL
GROUP BY away_team_id
) x
GROUP BY team_id
ORDER BY points DESC;
This should be the expected result:
Any idea on what I am doing wrong? Thanks!
Update 1:
Trying out Dans query doesn't work:
select team.name, HomePoints + AwayPoints points from team join ( select team.id, sum(case when home.home_score > home.away_score then 3 when home.home_score = home.away_score then 1 else 0 end) HomePoints, sum(case when away.away_score > away.home_score then 3 else 0 end) AwayPoints from team join matches home on team.id = home.home_team_id join matches away on team.id = away.away_team_id WHERE home.league_id = 94 AND home.season_id = 82 AND home.confirmed IS NOT NULL group by id ) temp on team.id = temp.id order by points desc;
I get this result: