title: SQL: finding longest date gap
body: <p>I have a table with 2 fields: unique ID, user ID (foreign key) and date-time. This is an access-log to a service. I work in SQL Server but I would appreciate agnostic answers.</p>
  
  <p>I would like using SQL to find for a certain user the ID from where the longest gap begins.</p>
  
  <p>So for example, say my values are as follows (simplification for one user):</p>
  
  <pre><code>ID |  User-ID |  Time
  ----------------------------------
  1  |  1       |  11-MAR-09, 8:00am
  2  |  1       |  11-MAR-09, 6:00pm
  3  |  1       |  13-MAR-09, 7:00pm
  4  |  1       |  14-MAR-09, 6:00pm
  </code></pre>
  
  <p>If I search for the longest gap for user 1 I will get ID 2 (it would also be nice to get the length of the gap right there and then, but much less critical).</p>
  
  <p>What's the most efficient way to achieve this in SQL?</p>
  
  <p>Note: ID is not necessarily sequential.</p>
  
  <p>Thank you</p>