I have a MySQL table looking like this:
I want to find a query that groups my table like this:
Details:
a_id = delimited area on a map
is_flag = 1-if sensor is in area/ 0 - if sensor not in area
Basically the first table describes in which area my sensor was at each timestamp.
The second table tells me the period that my sensor stayed in and out of each area.
I am using the below query for each areas_id with union all in order to output in a single table, time periods of how my sensor traveled between areas and how much it stayed in/out each area.
select t.a_id, min(t.timestamp) starttime,max(t.timestamp) endtime,
t.is_flag from(SELECT *,
ROW_NUMBER() OVER(ORDER BY a.timestamp) - ROW_NUMBER() OVER(PARTITION BY
a.is_flag ORDER BY a.timestamp) as GRP
FROM tablename a where areas_id=25 ) t
group by is_flag , GRP, a_id
Here is my dbfiddle: https://www.db-fiddle.com/f/5pHiYKyx4yHoirRbGX4kP4/0
My query does what I need, but it takes to long for a whole day.

