select CASE
WHEN goals_scored = 0 THEN '0'
WHEN goals_scored = 1 THEN '1'
WHEN goals_scored = 2 THEN '2'
ELSE '3+'
END AS goals_bucket, count(match_id) as nr_matches
from
(select m.match_id, SUM(CASE WHEN mps.goals_scored IS NOT NULL THEN mps.goals_scored ELSE 0 END) as goals_scored
, SUM(CASE WHEN mps.goals_assists IS NOT NULL THEN mps.goals_assists ELSE 0 END) as goals_assists
, COUNT(distinct m.match_id) as total_matches
from @blt.ultimate_soccer_dataset.seasons s
inner join @blt.ultimate_soccer_dataset.competitions c on (s.competition_id = c.competition_id)
inner join @blt.ultimate_soccer_dataset.matches m on (m.season_id = s.season_id)
left outer join @blt.ultimate_soccer_dataset.match_player_stats mps on (mps.match_id = m.match_id)
where slug = 'premier-league' and year(s.start_date) >= 2015 and m.status = 'Match Finished'
group by all
)
group by all
order by nr_matches desc