select count(*) from ( select e1.*,(select e2."time" from events as e2 where e2.cam_uuid=e1.cam_uuid and e2."id" > e1."id" ORDER BY e2."id" LIMIT 1) as "NextTime" from events as e1 where e1.event = 'Continuous' and e1.time > '2023-02-06' and e1.time < '2023-02-07' ) e3 WHERE cast(extract(epoch from e3."NextTime" ) as INTEGER) - cast(extract(epoch from e3."time_end") as INTEGER) > 5 select "id" ,cam_uuid,"time",time_end,"NextTime", "NextTime" - time_end from ( select e1.*,(select e2."time" from events as e2 where e2.cam_uuid=e1.cam_uuid and e2."id" > e1."id" ORDER BY e2."id" LIMIT 1) as "NextTime" from events as e1 where e1.event = 'Continuous' and e1.time > '2023-02-06' and e1.time < '2023-02-07' ) e3 WHERE cast(extract(epoch from e3."NextTime" ) as INTEGER) - cast(extract(epoch from e3."time_end") as INTEGER) > 5 select count(*) from events e where recording_type ='Continuous' and time_end > now() - interval '2 minute'