Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. SELECT host_team, 3 as points
  2. FROM matches where host_goals > guest_goals
  3. UNION
  4. SELECT guest_team, 3 as points
  5. FROM matches where host_goals < guest_goals
  6. UNION
  7. SELECT host_team, 1 as points
  8. FROM matches where host_goals = guest_goals
  9. UNION
  10. SELECT guest_team, 1 as points
  11. FROM matches where host_goals = guest_goals
  12. ;with results as(
  13. SELECT host_team as team_id, 3 as points
  14. FROM matches where host_goals > guest_goals
  15. UNION
  16. SELECT guest_team as team_id, 3 as points
  17. FROM matches where host_goals < guest_goals
  18. UNION
  19. SELECT host_team as team_id, 1 as points
  20. FROM matches where host_goals = guest_goals
  21. UNION
  22. SELECT guest_team as team_id, 1 as points
  23. FROM matches where host_goals = guest_goals
  24. ), totals as (
  25. SELECT team_id, SUM(points) as num_points
  26. from results
  27. GROUP BY team_id
  28. )
  29. SELECT team_id, sum(points)
  30. FROM matches,
  31. LATERAL (
  32. SELECT host_team AS team_id, CASE WHEN host_goals>guest_goals THEN 3 WHEN host_goals<guest_goals THEN 0 ELSE 1 END AS points
  33. UNION ALL
  34. SELECT guest_team, CASE WHEN host_goals>guest_goals THEN 0 WHEN host_goals<guest_goals THEN 3 ELSE 1 END
  35. ) t
  36. GROUP BY team_id
  37. ORDER BY sum DESC;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement