The following query shows login information based on the userId. If a user closes their browser without logging off or the session expires, the logoff_date will remain null as in the example below.
userId logon_date logoff_date
1 2012-01-01 10:00:00 2012-01-01 12:00:00
1 2012-01-01 09:00:00 NULL
Because there is a newer logon_date of 2012-01-01 10:00:00, I know that the user must have killed the session for the login_date of 2012-01-01 09:00:00.
Here is my query:
SELECT userId, logon_date, logoff_date
FROM user_logon
WHERE user_id = 2
What I would like is to count only active sessions. In order to do this, I need to skip the rows where the logoff_date is missing if there is a newer row with the same userId.
How about this:
?
PS: This query implies that for each date there is only one record for particular user