I am working on application related to GeoFencing software.I am recieiving realtime location Coordinates data from mobile GPS,which I am processing in Backend server and storing in Database server.I am categorizing a location point into 2 types.one is “Geofence” Point which is inside a geofence and other one is “ExtraLocation” point.So,for Extralocation points ,I am finding address and storing in database table named locations_raw and for points within Geofence,I am storing activity_id.Need help in writing a sql query to find locations where user has stopped for more than 3 minutes outside of Geofence?
The query I have written is
“SELECT task_id, address, MIN(created_at) as start_time, MAX(created_at) as end_time, count(*) as total_count FROM locations_raw WHERE activity_id = ” GROUP BY task_id, address HAVING EXTRACT(EPOCH FROM (MAX(created_at) – MIN(created_at))) /60 > 3″.
The problem arises when I revisit already visited address again,since we are grouping by task_id,address the address end_time will be now revisited endtime,The time difference will obviously be more than 3 minutes.And the address (which we revisited) is being processed as stoppage time,even though it isn’t.How can I modify the above query to get correct results?