Using a PHP Framework called Symfony2, I am developing a web application for people who play the video game Fifa to compete against each other in an online competition. It’s on Github: https://github.com/JonnyD/Elite-Fifa-Leagues
In the picture above you can see the UI of the league standings. These values are calculated and stored in a Standings table after every confirmed match. There’s a problem with this and that is storing calculated values breaks normalization. The only time this could be acceptable is in cases where you want to improve performance by not having to re-calculate the values every time you need them.
However, what if I want to find out a teams standing by their Last X Games Played at Home, Last X Games Played Away, or Last X Games Played Combined?
Here’s how I could get Standings by home matches only:
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;
Here’s how I could get Standings by Away matches only:
```
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;
Here’s how I could get Standings by Home and Away matches combined:
SELECT team.name,
team_id AS team_id,
COUNT(*) AS played,
SUM((CASE WHEN team_score > other_team_score THEN 1 ELSE 0 END)) AS won,
SUM((CASE WHEN team_score < other_team_score THEN 1 ELSE 0 END)) AS lost,
SUM((CASE WHEN team_score = other_team_score THEN 1 ELSE 0 END)) AS drawn,
SUM(team_score) AS goalsFor,
SUM(other_team_score) AS goalsAgainst,
SUM(team_score - other_team_score) AS goalDifference,
SUM((CASE WHEN team_score > other_team_score THEN 3
WHEN team_score = other_team_score THEN 1
ELSE 0 END)) AS points
FROM
(
-- LIST TEAM STATS WHEN PLAYED AS HOME_TEAM
SELECT
id,
league_id,
season_id,
home_team_id as team_id,
home_score as team_score,
away_score as other_team_score,
confirmed
FROM matches
UNION ALL
-- LIST TEAM STATS WHEN PLAYED AS AWAY_TEAM
SELECT
id,
league_id,
season_id,
away_team_id as team_id,
away_score as team_score,
home_score as other_team_score,
confirmed
FROM matches
) matches
INNER JOIN team ON matches.team_id = team.id
WHERE league_id = 94
AND season_id = 82
AND confirmed IS NOT NULL
GROUP BY team.name, team_id
ORDER BY POINTS DESC;