title: How to make a sql loop query to check difference between values?
body: <p>first of all, I'm not a native english speaker and this is a hard to explain issue. If you have any doubts, please let me know.</p>
  
  <p>We're using a GPS vehicle tracking device, which is programmed to send and store in a DB the position of the vehicle every 5 min when not moving, and every 100 meters when moving.</p>
  
  <p>This DB has a table called "vehicle_gps" that stores the data, with values such as speed, position, datetime, vehicle_gps_id.</p>
  
  <p>I need some kind of query that will show the different positions where the vehicle stopped for more than a certain amount of time(e.g 2 minutes, discarding traffic lights). </p>
  
  <p>For instance, I need something that can tell me the following:
  "The vehicle was on Positon1 (P1) at 8:00, it left P1 towards P2, reaching it at 8.20. The vehicle stayed at P2 until 10.20 and it reached P3 at 10.50"</p>
  
  <p>example of the records in the table</p>
  
  <pre><code>vehicle_gps_id  | datetime---------- | latitude | longitude | speed
  
  1000------------| 05/16/2012 08:00:00|50.0000   |50.00000   |40 (km/h)
  1001------------| 05/16/2012 08:01:00|51.0000   |51.00000   |38 (km/h)
  1002------------| 05/16/2012 08:01:23|51.0045   |50.000054  |40 (km/h)
  1003------------| 05/16/2012 08:01:40|51.00540  |51.0005430 |39 (km/h)
  .
  .
  .
  1040------------| 05/16/2012 08:20:40|53.00540  |53.0005430 |0 (km/h)
  1041------------| 05/16/2012 08:25:40|53.00540  |53.0005430 |0 (km/h)
  1042------------| 05/16/2012 08:30:40|53.00540  |53.0005430 |0 (km/h)
  .
  .
  .
  1060------------| 05/16/2012 10:20:40|53.00540|53.0005430|20 (km/h)
  </code></pre>
  
  <p>How can I do something like that?</p>
  
  <p>So far, I was able to get the difference in minutes between fixed positions and for that reason, I wanted a loop that would check all dates between positions and break whenever that difference was longer than 5 minutes, which means that the vehicle had stopped.</p>
  
  <p>TIA</p>