SELECT
YEAR(l.created_at) as year,
MONTH(l.created_at) as month,
UPPER(l.state) as state,
n.slug as vertical,
CAST(SUM(3 - l.sentto) AS SIGNED) as count
FROM leads l
JOIN niches n ON l.niche_id = n.id
WHERE l.sentto < 3
AND l.validation_status IN ('accepted', 'sms_validated')
AND l.created_at >= '2020-01-01'
AND l.state IN ('NSW', 'VIC', 'QLD', 'WA', 'SA', 'TAS', 'ACT', 'NT')
GROUP BY year, month, l.state, n.slug
ORDER BY year DESC, month DESC, state ASC;