Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SELECT p.team_name,final_table.Score
- from TEAMS p,
- (
- SELECT team.teamid TeamId,sum(team.Score) Score
- from(
- (SELECT home.team1_id TeamId,sum(home.Score) Score
- from(
- select team1_id,
- case
- WHEN goals_team1 > goals_team2 THEN 3
- WHEN goals_team1 = goals_team2 THEN 1
- ELSE 0
- END Score
- from RESULTS
- )home
- group by home.team1_id)
- union all
- (SELECT away.team2_id TeamId,sum(away.Score) Score
- from(
- select team2_id,
- case
- WHEN goals_team2 > goals_team1 THEN 3
- WHEN goals_team2 = goals_team1 THEN 1
- ELSE 0
- END Score
- from RESULTS
- )away
- group by away.team2_id)) team
- group by team.teamid
- order by Score desc
- )final_table
- where p.team_id = final_table.teamid
- order by Score desc;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement