select roomid,starttime starttime, endtime endtime from (
select
if(@roomid=roomid,@d,'') as starttime,@d:=str_to_date(timepoint,'%Y-%m-%d %H:%i:%s.%f'),@roomid:=roomid,p.roomid,str_to_date(timepoint,'%Y-%m-%d %H:%i:%s.%f') endtime
from tmp_time_point p,(select @d:='',@roomid:=-1) vars
orderby roomid,timepoint
) v4 where starttime!=''anddate(starttime)=date(endtime);
select roomid,date(s) dt,round(second,date_format(s,'%Y-%m-%d %H:%i:%s'),date_format(e,'%Y-%m-%d %H:%i:%s')))/60) ts,max(num) c from
(
select a.roomid,num,a.s,a.e from (
selectwhen @roomid=roomid anddate(@timepoint)=date(timepoint) then @num:=@num+prevC when @roomid:=roomid then @num:=0 end num,@timepoint:=timepoint ,a.* from (
selectwhen @roomid=roomid then @prevC when @roomid:=roomid then @prevC:=0 end prevC,@prevC:=c,b.* from (
select * from t2 ,(select @roomid:=-1,@timepoint:='',@num:=0,@prevC:=-1) vars
) b orderby roomid,timepoint
) a orderby roomid,timepoint
) c
innerjoin
tmp_min_range a on( c.timepoint=a.e and c.roomid=a.roomid)