Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

sql para calcular pontos para jogos


Você pode fazer isso usando UNION :
SELECT team, SUM(points) AS total_points
FROM (
  SELECT team_home AS team, 
         CASE 
            WHEN team_home_goals > team_away_goals THEN 3
            WHEN team_home_goals = team_away_goals THEN 1
            ELSE 0
         END AS points
  FROM games

  UNION ALL

  SELECT team_away AS team, 
         CASE 
            WHEN team_away_goals > team_home_goals THEN 3
            WHEN team_away_goals = team_home_goals THEN 1
            ELSE 0
         END AS points
  FROM games ) AS t
GROUP BY team
ORDER BY total_points DESC

Demonstração aqui