Loading...Loading chart...
1WITH team_stats AS (
2 SELECT
3 t.name as team_name,
4 COUNT(*) as matches_played,
5 SUM(CASE
6 WHEN (m.home_team_id = t.team_id AND m.home_score > m.away_score)
7 OR (m.away_team_id = t.team_id AND m.away_score > m.home_score)
8 OR (m.home_team_id = t.team_id AND m.home_penalty_score > m.away_penalty_score)
9 OR (m.away_team_id = t.team_id AND m.away_penalty_score > m.home_penalty_score)
10 OR (m.home_team_id = t.team_id AND m.home_extratime_score > m.away_extratime_score)
11 OR (m.away_team_id = t.team_id AND m.away_extratime_score > m.home_extratime_score)
12 THEN 1 ELSE 0 END) as wins,
13 SUM(CASE
14 WHEN m.home_team_id = t.team_id THEN m.away_score
15 WHEN m.away_team_id = t.team_id THEN m.home_score
16 ELSE 0 END) as goals_conceded
17 FROM @blt.ultimate_soccer_dataset.teams t
18 JOIN @blt.ultimate_soccer_dataset.matches m ON t.team_id = m.home_team_id OR t.team_id = m.away_team_id
19 JOIN @blt.ultimate_soccer_dataset.seasons s ON m.season_id = s.season_id
20 JOIN @blt.ultimate_soccer_dataset.competitions c ON s.competition_id = c.competition_id
21 WHERE c.name = 'FIFA Club World Cup'
22 AND m.status = 'Match Finished'
23 GROUP BY t.name
24)
25SELECT
26 team_name,
27 matches_played,
28 wins,
29 ROUND(wins * 100 / matches_played, 0) as win_percentage,
30 goals_conceded
31FROM team_stats
32ORDER BY win_percentage DESC
33LIMIT 5