'Calculating time difference between two logins [duplicate]

I want to calculate the time spent by user on our system. In logs table we track currently login time but we don't have logout time logged currently. So to find out time spent by user in a session I want to find out difference between two login events

Example

For example user has logged in multiple times on 2022-02-16 so I have to calculate the time difference between two login events. How we can achieve in that SQL Server? I have to do this inside a view, so can not use any temp tables or SP



Solution 1:[1]

You want the Datediff(minutes, time1, time2)

Instead of minutes use the units you want to measure in.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mike Wodarczyk