Lloyd,
A small improvement is to limit the number of rows used to test for event existence. This avoid scanning the whole event_data table if there are many events.
select minutes.i as minute, count(*) as events
from
(select (event_time / 60) % 60 as begin_minute, ((event_time + duration) / 60) % 60 as end_minute
from event_data)
outer join integers60 as minutes
where
case
when begin_minute <= end_minute then
begin_minute <= minutes.i and minutes.i <= end_minute
else
begin_minute <= minutes.i or minutes.i <= end_minute
end
group by minute
union select 0, 0 where not exists (select * from event_data limit 1)
HTH Dennis Cote