use tempdb GO CREATE TABLE T (computername VARCHAR(100),currentuser VARCHAR(100), DT datetime, TheOrder INT) GO /*************** Viorel-1: test 1 *******************/ TRUNCATE TABLE T GO INSERT T(computername, currentuser, DT, TheOrder) VALUES ('abc', 'xyz', CONVERT(DATETIME,'7/5/2022 20:04:51',101),1), ('abc', 'xyz', CONVERT(DATETIME,'7/5/2022 20:04:51',101),1), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 6:45:51',101),1), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 6:45:51',101),1), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 7:06:45',101),2), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 7:06:45',101),3), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 7:07:00',101),4), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 7:59:12',101),2), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 7:59:12',101),3), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 7:59:19',101),4), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 7:59:21',101),5), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 21:28:19',101),1), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 21:28:19',101),1), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 21:28:24',101),2), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 21:28:24',101),3), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 21:28:24',101),4) GO ; with Q1 as ( select *, lag([TheOrder]) over (partition by computername, currentuser, cast([DT] as date) order by [DT]) po, lead([TheOrder]) over (partition by computername, currentuser, cast([DT] as date) order by [DT]) no from T ), Q2 as ( select *, case when po is null or po > [TheOrder] then [DT] end as starttime, case when no is null or [TheOrder] > no then [DT] end as endtime from Q1 ), Q3 as ( select * from Q2 where starttime is not null or endtime is not null ), Q4 as ( select computername, currentuser, starttime, lead(endtime) over (partition by computername, currentuser, cast([DT] as date) order by [DT]) as endtime from Q3 ) select * from Q4 where starttime is not null GO /*************** Viorel-1: test 2 *******************/ TRUNCATE TABLE T GO INSERT T(computername, currentuser, DT, TheOrder) VALUES ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 7:07:00',101),4), -- moving this row to the start ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 7:59:12',101),3), -- moving this ro ('abc', 'xyz', CONVERT(DATETIME,'7/5/2022 20:04:51',101),1), ('abc', 'xyz', CONVERT(DATETIME,'7/5/2022 20:04:51',101),1), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 6:45:51',101),1), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 6:45:51',101),1), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 7:06:45',101),2), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 7:06:45',101),3), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 7:59:12',101),2), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 7:59:19',101),4), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 7:59:21',101),5), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 21:28:19',101),1), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 21:28:19',101),1), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 21:28:24',101),2), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 21:28:24',101),3), ('abc', 'xyz', CONVERT(DATETIME,'7/6/2022 21:28:24',101),4) GO ; with Q1 as ( select *, lag([TheOrder]) over (partition by computername, currentuser, cast([DT] as date) order by [DT]) po, lead([TheOrder]) over (partition by computername, currentuser, cast([DT] as date) order by [DT]) no from T ), Q2 as ( select *, case when po is null or po > [TheOrder] then [DT] end as starttime, case when no is null or [TheOrder] > no then [DT] end as endtime from Q1 ), Q3 as ( select * from Q2 where starttime is not null or endtime is not null ), Q4 as ( select computername, currentuser, starttime, lead(endtime) over (partition by computername, currentuser, cast([DT] as date) order by [DT]) as endtime from Q3 ) select * from Q4 where starttime is not null