Skip to content
Advertisement

How to calculate average time between entries grouped by date and user in SQL Server?

I have data that looks like:

I am trying to calculate the average length of time between the End_Time of one entry and the Start_time of the next entry, grouped by the user and the date.

From the example above the output should look like:

Is there a SQL Server query that could accomplish this?

Advertisement

Answer

Use lead():

Here is a db<>fiddle (with the data fixed so the end times are greater than the start times).

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement