I am wondering if its possible to implement an SQL Query that will act like a sort of algorithm to calculate a certain figure for me based on the following table:
This is the initial query,
SELECT Activity, TimeOfAction, Requestor
FROM EventLog
WHERE Requestor = 0
ORDER BY Requestor, TimeOfAction;
And a sample of the data that is returned,
Login 2010-05-28 15:52:50.590 0
Login 2010-05-28 15:52:50.873 0
Logout 2010-05-28 15:52:50.890 0
Logout 2010-05-28 16:22:57.983 0
Login 2010-05-29 11:29:36.967 0
Logout 2010-05-29 11:29:37.640 0
As you can see there are duplicate logins and logouts in this dataset. I need to calculate the length of a session by taking the FIRST login and LAST logout when there are duplicates. So the first session given the data above would be from,
5-28 15:52:50.590 to 5-28 16:22:57.983
The algorithm is roughly,
1) Order a list of logins / logouts by username, then by time of action
2) If entry is a login, search for the next logout that is followed immediately by a login (to confirm it is the last logout of all duplicates)
3) Use first login and last logout to create a new session (length is logout time - login time)
4) Repeat
Currently I am just implementing this in code but was wondering if its even possible in SQL (I'm not too familiar with SQL).