I have a visits table (id int, start datetime, end datetime), and I you wish to track peak visit counts.
Example data:
+------+---------------------+----
This is not very efficient, but it'll give you your result:
select U.dt1 as date-time-1, DATE_ADD(U.dt2,INTERVAL -1 SECOND) as date-time-2,
(select count(id) from Visits where
(dt1 >= u.dt1 and dt1=u.dt2) -- dt1()dt2
--or (dt2 >= u.dt1 and dt2 A.dt1 union select min(dt1) as dt from Visits where dt1 > A.dt1) M
) as dt2 from Visits A
union
select B.dt2 as dt1, (
select min(M.dt) from ( select min(dt2) as dt from Visits where dt2 > b.dt2 union select min(dt1) as dt from Visits where dt1 > b.dt2) M
) as dt2 from Visits b where B.dt2 <> (select max(dt2) from Visits)
) U
I've commented the condition checking to see if a visit starts before a range and ends within it to get the same result set as you but I believe you should consider this.