I’m not sure what you’ve tried so far but based on your sample data, try this:
SELECT
UserID, SessionID,
MIN(timestamp) StartTime, MAX(timestamp) EndTime,
DATEDIFF(s,MIN(timestamp), MAX(timestamp)) SecondsDifference
FROM
Table
GROUP BY
UserID, SessionID
This kind of data is usually tricky because you don’t have the luxury of a sessionid, or the sessionid doesn’t truly reflect what you’re trying to measure
solved How to calculate time spent (Timestamp) query, in T-SQL , SQL Server