title: Get list with start and end values from table of datetimes
body: <p>Currently i have a table built up like this way</p>
  
  <pre><code>DeviceID      Timestamp            Value
  ----------------------------------------
  Device1       1.1.2011 10:00:00    3
  Device1       1.1.2011 10:00:01    4
  Device1       1.1.2011 10:00:02    4
  Device1       1.1.2011 10:00:04    3
  Device1       1.1.2011 10:00:05    4
  Device1       1.1.2011 14:23:14    8
  Device1       1.1.2011 14:23:15    7
  Device1       1.1.2011 14:23:17    4
  Device1       1.1.2011 14:23:18    2
  </code></pre>
  
  <p>As you can see there are coming in some values from a device with a given timestamp (the column type is datetime).</p>
  
  <p>The problem is that the device can be started and stopped at any point and there is no direct information within the data that a start or stop had occured. But from the list of given timestamps it is quite easy to tell when a start and stop had occured, due to the fact that whenever the timestamps of two rows is within five seconds, they belong to the same measurement.</p>
  
  <p>Now i'd like to get out of this data a list like this:</p>
  
  <pre><code>DeviceID      Started              Ended
  Device1       1.1.2011 10:00:00    1.1.2011 10:00:05
  Device1       1.1.2011 14:23:14    1.1.2011 14:23:18
  </code></pre>
  
  <p>So any ideas how to do this in a fast way? All i can think about is using some kind of cursor and compare each datetime pair by hand. But i think this will get really slow cause we have to inspect each value in each row.</p>
  
  <p>So is there any better SQL solution which won't work with cursors?</p>
  
  <h3>Update</h3>
  
  <p>Currently i tested all the given answers. And by reading they all look good and had some interesting approaches. Unfortunately all of them (so far) failed on the real data. The biggest problem seems to be the mass of the data (currently their are round about 3.5 millions entries in the table). Performing the given query only on a small subset leads to the expected results, but rolling the query onto the whole table just leads to a very bad performance.</p>
  
  <p>I have to further test and examine if i can chunkify the data and only pass a part of the data to one of these given algorithms to get this thing rolling. But maybe one of you has another smart idea to get the results a little bit faster.</p>
  
  <h3>Update (More informations about structure)</h3>
  
  <p>Okay, these informations might help, too:
  Currently there are round about 3.5 million entries in the table. And here are the given column types and indizes:</p>
  
  <ul>
  <li>_ID
  <ul>
  <li>int</li>
  <li>Primary Key</li>
  <li>Grouped Index</li>
  <li>didn't mentioned this column in my example, cause it isn't needed for this query</li>
  </ul></li>
  <li>DeviceID
  <ul>
  <li>int</li>
  <li>not null</li>
  <li>Index</li>
  </ul></li>
  <li>Timestamp
  <ul>
  <li>datetime</li>
  <li>not null</li>
  <li>Index</li>
  </ul></li>
  <li>Value
  <ul>
  <li>several not indexed columns of different types (int, real, tinyint)</li>
  <li>all can be null</li>
  </ul></li>
  </ul>
  
  <p>Maybe this helps to improve your already (or new) solutions to the given problem.</p>