With cte As
(Select EventDateTime, Computer, UserId, EventType,
Row_Number() Over (Partition By UserId, Computer Order By EventDateTime)
As rn
From YourTable
Where EventType In ('Login', 'Logout')
/* add other conditions as needed, like date time range
you are interested in */)
Select c1.UserId, c1.Computer, c1.EventDateTime
From cte c1
Left Join cte c2 On c1.UserId = c2.UserId
And c1.Computer = c2.Computer
And c1.rn = c2.rn - 1
And c2.EventType = 'Logout'
And DateDiff(ss, c1.EventDateTime, c2.EventDateTime) <= 46800 /* 13 hours
in seconds */
Where c1.EventType = 'Login'
And c2.UserId Is Null
And DateDiff(ss, c1.EventDateTime, Current_TimeStamp) > 46800
Order By c1.UserId, c1.EventDateTime;
Essentially, the above works by trying match every login to the suceending
row for that user and computer. If there isn't any match, then the
c2.UserId will be NULL. So those are the rows you want. Except you only
want rows where the login is more than 13 hours ago. This query probably
won;t be exqactly what you want. Hopefully, it will get you started.
Tom
"Ryan Gaudet" <RyanG...@discussions.microsoft.com> wrote in message
news:F8C261A2-5F96-4BFC...@microsoft.com...
"Tom Cooper" wrote:
> .
>