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.

I would like using SQL to find for a certain user the ID from where the longest gap begins.

So for example, say my values are as follows (simplification for one user):

```
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

```

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).

What's the most efficient way to achieve this in SQL?

Note: ID is not necessarily sequential.

Thank you