How do you partition an events table by a time based off the datediff(Lag,time)? I am not sure how really to ask this question as i have never come across such grouping/ partitioning before.

Basically i have an events table with 50 million rows that give me a timestamp, user, video and a event. A super basic example of my current query is:

Activity as (
Select 
Min(Timestamp) as start_Of_Activity,
Max(Timestamp) as End_Of_Activity
Session,
User,
Video,
Sum(Event_Count)
Group by User,Video,Session
)


I have just found out that the session ID is not accurate and wont be fixed so i have to change my business rules for defining an "Activity". Basically i have to run a Lag/Lead partition over each row to find out 'if The difference between a rows timestamp and the previous rows timestamp is over 2 hours for that user/video' then consider it another Row. Does anybody know how i should go about grouping this event data. 

Example

Raw data example

Session ID:   User:  Video:   Activity_Start_Time   Event
10             1       2        12/2/19T12.05.05     play
10             1       2        12/2/19T01.05.05     stop
10             1       2        14/2/19T12.05.05     play
10             1       2        14/2/19T01.05.05     Watching
10             1       2        16/2/19T02.10.10     Stop


Current Grouping:

Session ID:   User:  Video:   Activity_Start_Time   Activity_End_Time
10             1       2        12/2/19T12.05.05     16/2/19T05.10.10


What the grouping needs to be

Session ID:   User:  Video:   Activity_Start_Time   Activity_End_Time
10             1       2        12/2/19T12.05.05     12/2/19T01.05.05
10             1       2        14/2/19T12.05.05     16/2/19T02.10.10
You can look at the following:  (Notice I changed your sample date formats for this example)
This uses a CTE to get your dataset with the 'LAG'ged value for [Activity_Start_Time].  The LAG is performed as partitioned by User and ordered by the date

Table:

Create Table #events
(
SessionID Int,
[User] Int,
Video Int,
Activity_Start_Time SmallDateTime,
[Event] VarChar(20)
)
Insert Into #events Values
(10,1,2,'2019-02-12 12:05:05','play'),
(10,1,2,'2019-02-12 13:05:05','stop'),
(10,1,2,'2019-02-14 12:05:05','play'),
(10,1,2,'2019-02-14 13:05:05','Watching'),
(10,1,2,'2019-02-16 14:10:10','Stop')


Query:

With cte As
(
Select 
       Activity_Start = Lag(Activity_Start_Time,1,0) 
                        Over (Partition By [User] Order By Activity_Start_Time),
       Case When IsNull(Lag(Activity_Start_Time,1,0) 
                        Over (Partition By [User] Order By Activity_Start_Time),0) = 0 
            Then 0 
            Else DateDiff(Hour,Lag(Activity_Start_Time,1,0) 
                        Over (Partition By [User] Order By Activity_Start_Time),Activity_Start_Time)
       End As Diff_Hours,
       *
From 
       #events
)
Select 
      SessionID,
      [User], 
      Video,
      Diff_Hours,
      Activity_Start,
      Activity_Start_Time As Activity_End
From cte
Where Diff_Hours &gt; 2


Drop Table #events


Result:

SessionID   User    Video   Diff_Hours  Activity_Start      Activity_End
10          1       2       47          2019-02-12 13:05:00 2019-02-14 12:05:00
10          1       2       49          2019-02-14 13:05:00 2019-02-16 14:10:00