Mysql
 sql >> Database >  >> RDS >> Mysql

sql per il calcolo dei punti per i giochi

Puoi farlo 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

Dimostrazione qui