Question

I have a mysql table with data from car gps tracker (lat,lng,speed,time). I want to select positions (latitude,longitude and time) where car has been stopped (speed = 0) longer than 10 min, 30 min, 1 hour etc.

My table looks like this:

id  latitude            longitude           speed   time
-----------------------------------------------------------------------
304 52.388983333333336  17.025338333333334  33.67   2014-03-26 08:00:04
305 52.39029            17.023776666666667  34.65   2014-03-26 08:00:14
306 52.391035           17.021631666666668  32.91   2014-03-26 08:00:24
307 52.39103166666666   17.01917            30.03   2014-03-26 08:00:34
308 52.39089833333333   17.01698            29.33   2014-03-26 08:00:44
309 52.390593333333335  17.01532            9.54    2014-03-26 08:00:54
310 52.39071333333333   17.015056666666666  0       2014-03-26 08:01:04
311 52.39105333333333   17.01499            10.3    2014-03-26 08:01:14
312 52.391485           17.01488            7.82    2014-03-26 08:01:24
313 52.391705           17.014815           0       2014-03-26 08:01:34
314 52.391705           17.014815           0       2014-03-26 08:01:44
315 52.391705           17.014815           0       2014-03-26 08:01:54
316 52.391705           17.014815           0       2014-03-26 08:02:04
317 52.391705           17.014815           0       2014-03-26 08:02:14
318 52.391705           17.014815           0       2014-03-26 08:02:24
319 52.39232333333333   17.014648333333334  7.12    2014-03-26 08:02:34
320 52.392345           17.014635           0       2014-03-26 08:02:44
321 52.392345           17.014635           0       2014-03-26 08:02:54
322 52.392345           17.014635           0       2014-03-26 08:03:04
323 52.392345           17.014635           0       2014-03-26 08:03:15
324 52.392345           17.014635           0       2014-03-26 08:03:25
325 52.392345           17.014635           0       2014-03-26 08:03:35
326 52.392558333333334  17.014471666666665  14.11   2014-03-26 08:03:45
327 52.392316666666666  17.012883333333335  27.47   2014-03-26 08:03:55
328 52.39194333333333   17.010871666666667  28.93   2014-03-26 08:04:05
329 52.39152333333333   17.00893            22.28   2014-03-26 08:04:15
330 52.391575           17.007181666666668  27.01   2014-03-26 08:04:25
331 52.39164            17.00501            26.48   2014-03-26 08:04:35
332 52.39159333333333   17.002895           28.34   2014-03-26 08:04:45
333 52.391641666666665  17.000795           26.39   2014-03-26 08:04:55
334 52.392156666666665  16.999178333333333  16.56   2014-03-26 08:05:05
335 52.39223666666667   16.998796666666667  0       2014-03-26 08:05:15
336 52.39234            16.99819            15.38   2014-03-26 08:05:25
337 52.39261166666667   16.996865           17.1    2014-03-26 08:05:35
338 52.392896666666665  16.995643333333334  20.91   2014-03-26 08:05:45
339 52.39313666666666   16.99468            7.5     2014-03-26 08:05:55
340 52.39331833333333   16.993918333333333  9.1     2014-03-26 08:06:05
341 52.3936             16.992806666666667  16.86   2014-03-26 08:06:15
342 52.393746666666665  16.992065           4.72    2014-03-26 08:06:25

Looking for help, any ideas?

Was it helpful?

Solution

You need to add an identifier to each "0" record so they can be grouped together. One simple measure is the number of non-zero records before it in the data.

select latitude, longitude, min(time) as StartTime, max(time) as EndTime
from (select t.*,
             (select count(*)
              from tracker t2
              where t2.time <= t.time and
                    t2.speed > 0
             ) as grp
      from tracker t
     ) t
group by grp, latitude, longitude;

You can then use a having clause to get the rows you are interested in:

having max(time) >= min(time) + interval 1 hour

For instance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top