I want to be able to calculate the total time between certain events in SQL Server 2008.
I have a table as follows:
UserId Event EventTimestamp
+-------+----------+---------------+
1 CheckedIn 14-05-15 10:01
2 CheckedIn 14-05-15 10:15
3 CheckedIn 14-05-15 10:17
1 CheckedOut 14-05-15 10:25
1 Logout 14-05-15 10:26
If you take a look at the table, a user can check-in, then can check-out and then go offline.
I want to calculate the time between a person checking-in to the point they check-out grouped by each week where the duration (between check-in and check-out) is less than 10 minutes.
So far I've managed to group by week, but I can't seem to calculate the duration between those events (which will happen in that order)...
select
'Week ' + cast(datepart(wk, EventUpdateStamp) as varchar(2)) Week,
UserId,
Event
from MyTable
group by datepart(wk, EventUpdateStamp), UserId, Event
I would like to have the results like this:
UserId TotalSessionCheckInTimeMinutes WeekNumber
+-------+------------------------------+---------------+
1 24 43
Taking the first row as an example:
- 1 is the User's ID
- 24 is the total duration in minutes between his check-in and check-out
- 43 is the week number in the year
I only want to include check-in to check-out. Don't want to measure between check out and logout (in other words, measure how long he was checked-in for).