ステップバイステップでやってみましょう:
自宅で勝ったゲームと自宅でのスコアを選択します:
SELECT COUNT(*) as wins, SUM(G.home_score) as score FROM games G WHERE
G.team_id = T.team_id #See 3. query and you'll understand
G.home_score > away_score
この結果をHOME_GAMESと呼びましょう。
勝ったゲームとアウェイゲームのスコアを選択します:
SELECT COUNT(*) as wins, SUM(G.away_score) as score FROM games G
WHERE
G.team_id = T.team_id #See 3. query and you'll understand
G.away_score > G.home_score
この結果をAWAY_GAMESと呼びましょう。
勝ったゲームの合計とスコアの合計を選択します:
SELECT (A.wins + H.wins) AS total_wins, (A.score + H.score) AS total_score FROM
(AWAY_GAMES) AS A, (HOME_GAMES) AS H, teams T
ORDER BY total_wins, total_score
==> AWAY_GAMESとHOME_GAMESを置き換えてすべてをまとめます:
SELECT (A.wins + H.wins) AS total_wins, (A.score + H.score) AS total_score FROM
(SELECT COUNT(*) as wins, SUM(G.away_score) as score FROM games G
WHERE
G.team_id = T.team_id #See 3. and you'll understand
G.away_score > G.home_score) AS A,
(SELECT COUNT(*) as wins, SUM(G.home_score) as score FROM games G
WHERE
G.team_id = T.team_id #See 3. and you'll understand
G.home_score > away_score) AS H,
teams T
ORDER BY total_wins, total_score