SELECT sum(mun),max from (
select (ceil(distance/500)-1)*500 as min,
ceil(distance/500)*500 as max,
count(*) mun
from tablename where date_format(alarm_date, '%Y-%m-%d') =date_format('2023-02-03', '%Y-%m-%d') group by ceil(distance/20)
) t GROUP BY max
;