I've been trying to get the following select statement to work for a few days now but haven't had any luck. A quick summary of the code is that it returns a list of all teams in the database and how many wins each team has against teams ranked in the top 10. The wins are pulled from the games table which contains every game that has been played. Here is the code I have
SELECT IF(@last_ranking <> formula, @current_rankings := @current_rankings + 1, @current_rankings) AS rank,
@last_ranking := formula,
prequery.team,
prequery.formula
FROM (select @current_rankings := 0) sqlvars,
(SELECT team,
SUM(score) formula
FROM (SELECT g.home_team,
SUM(CASE WHEN t.team_id IS NULL THEN 0 ELSE g.home_wins END) AS formula
FROM games g LEFT JOIN
_top_10 AS t ON g.away_team = t.team_id
WHERE
g.season = 2012 AND
g.completed = 1
UNION ALL SELECT g.away_team,
SUM(CASE WHEN t2.team_id IS NULL THEN 0 ELSE g.away_wins END) AS formula
FROM games g LEFT JOIN
_top_10 AS t2 ON g.home_team = t2.team_id
WHERE
g.season = 2012 AND
g.completed = 1),
(SELECT @current_rankings := 0, @last_ranking := 0) r
GROUP BY team ORDER BY formula DESC ) prequery;
but I keep getting the following error
Error Code: 1248. Every derived table must have its own alias
Can anyone see what I'm doing wrong here. Thanks!
probably you miss to add the alias because of the alignment of your
SELECT
statements.