Merge time slots where there are no gaps greater than slot allocation

Hello ,

I have a query that generates time slots of 5 min between a day and i want to join the slot together where there isnt a gap of greater than 5 min, would like to group them by the user id.

i have some sample data attached.

So based on the userid group the slots together where there is no gap.

thanks
ExampleData.xlsx
deanmachine333Asked:
Who is Participating?
 
Nico BontenbalConnect With a Mentor Commented:
Very clever solution. I misunderstood your specifications. I thought the 'gap' was the time between the end time of one session and the start time of the next session. But it seems like you want to merge the slots when the end time of one slot equals the start time of the next. With your sample data this query returns the same result:

create table #Sessions(UserName varchar(10),StartTime  datetime, EndTime  datetime)
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 09:00','20160826 09:05')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 09:00','20160826 09:05')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 09:00','20160826 09:05')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 09:00','20160826 09:05')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 09:00','20160826 09:05')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 09:05','20160826 09:10')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 09:05','20160826 09:10')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 09:05','20160826 09:10')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 09:05','20160826 09:10')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 09:05','20160826 09:10')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 09:10','20160826 09:15')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 09:10','20160826 09:15')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 09:10','20160826 09:15')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 09:10','20160826 09:15')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 09:10','20160826 09:15')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 09:15','20160826 09:20')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 09:15','20160826 09:20')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 09:15','20160826 09:20')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 09:15','20160826 09:20')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 09:15','20160826 09:20')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 09:20','20160826 09:25')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 09:20','20160826 09:25')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 09:20','20160826 09:25')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 09:20','20160826 09:25')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 09:20','20160826 09:25')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 09:20','20160826 09:25')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 09:25','20160826 09:30')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 09:25','20160826 09:30')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 09:25','20160826 09:30')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 09:25','20160826 09:30')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 09:25','20160826 09:30')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 09:25','20160826 09:30')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 09:30','20160826 09:35')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 09:30','20160826 09:35')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 09:30','20160826 09:35')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 09:30','20160826 09:35')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 09:30','20160826 09:35')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 09:30','20160826 09:35')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 09:35','20160826 09:40')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 09:35','20160826 09:40')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 09:35','20160826 09:40')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 09:35','20160826 09:40')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 09:35','20160826 09:40')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 09:35','20160826 09:40')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 09:40','20160826 09:45')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 09:40','20160826 09:45')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 09:40','20160826 09:45')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 09:40','20160826 09:45')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 09:40','20160826 09:45')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 09:40','20160826 09:45')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 09:45','20160826 09:50')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 09:45','20160826 09:50')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 09:45','20160826 09:50')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 09:45','20160826 09:50')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 09:45','20160826 09:50')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 09:45','20160826 09:50')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 09:50','20160826 09:55')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 09:50','20160826 09:55')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 09:50','20160826 09:55')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 09:50','20160826 09:55')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 09:50','20160826 09:55')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 09:50','20160826 09:55')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 09:55','20160826 10:00')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 09:55','20160826 10:00')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 09:55','20160826 10:00')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 09:55','20160826 10:00')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 09:55','20160826 10:00')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 09:55','20160826 10:00')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 10:00','20160826 10:05')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 10:00','20160826 10:05')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 10:00','20160826 10:05')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 10:00','20160826 10:05')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 10:00','20160826 10:05')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 10:05','20160826 10:10')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 10:05','20160826 10:10')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 10:05','20160826 10:10')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 10:05','20160826 10:10')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 10:05','20160826 10:10')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 10:10','20160826 10:15')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 10:10','20160826 10:15')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 10:10','20160826 10:15')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 10:10','20160826 10:15')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 10:10','20160826 10:15')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 10:15','20160826 10:20')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 10:15','20160826 10:20')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 10:15','20160826 10:20')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 10:15','20160826 10:20')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 10:15','20160826 10:20')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 10:20','20160826 10:25')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 10:20','20160826 10:25')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 10:20','20160826 10:25')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 10:20','20160826 10:25')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 10:20','20160826 10:25')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 10:20','20160826 10:25')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 10:25','20160826 10:30')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 10:25','20160826 10:30')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 10:25','20160826 10:30')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 10:25','20160826 10:30')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 10:25','20160826 10:30')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 10:25','20160826 10:30')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 10:30','20160826 10:35')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 10:30','20160826 10:35')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 10:30','20160826 10:35')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 10:30','20160826 10:35')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 10:30','20160826 10:35')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 10:30','20160826 10:35')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 10:35','20160826 10:40')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 10:35','20160826 10:40')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 10:35','20160826 10:40')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 10:35','20160826 10:40')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 10:35','20160826 10:40')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 10:35','20160826 10:40')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 10:40','20160826 10:45')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 10:40','20160826 10:45')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 10:40','20160826 10:45')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 10:40','20160826 10:45')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 10:40','20160826 10:45')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 10:40','20160826 10:45')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 10:45','20160826 10:50')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 10:45','20160826 10:50')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 10:45','20160826 10:50')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 10:45','20160826 10:50')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 10:45','20160826 10:50')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 10:45','20160826 10:50')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 10:50','20160826 10:55')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 10:50','20160826 10:55')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 10:50','20160826 10:55')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 10:50','20160826 10:55')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 10:50','20160826 10:55')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 10:50','20160826 10:55')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 10:55','20160826 11:00')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 10:55','20160826 11:00')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 10:55','20160826 11:00')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 10:55','20160826 11:00')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 10:55','20160826 11:00')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 10:55','20160826 11:00')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 11:00','20160826 11:05')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 11:00','20160826 11:05')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 11:00','20160826 11:05')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 11:00','20160826 11:05')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 11:05','20160826 11:10')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 11:05','20160826 11:10')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 11:05','20160826 11:10')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 11:05','20160826 11:10')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 11:10','20160826 11:15')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 11:10','20160826 11:15')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 11:10','20160826 11:15')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 11:10','20160826 11:15')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 11:15','20160826 11:20')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 11:15','20160826 11:20')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 11:15','20160826 11:20')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 11:15','20160826 11:20')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 11:20','20160826 11:25')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 11:20','20160826 11:25')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 11:20','20160826 11:25')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 11:20','20160826 11:25')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 11:20','20160826 11:25')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 11:20','20160826 11:25')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 11:25','20160826 11:30')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 11:25','20160826 11:30')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 11:25','20160826 11:30')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 11:25','20160826 11:30')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 11:25','20160826 11:30')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 11:25','20160826 11:30')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 11:30','20160826 11:35')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 11:30','20160826 11:35')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 11:30','20160826 11:35')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 11:30','20160826 11:35')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 11:30','20160826 11:35')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 11:35','20160826 11:40')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 11:35','20160826 11:40')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 11:35','20160826 11:40')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 11:35','20160826 11:40')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 11:35','20160826 11:40')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 11:40','20160826 11:45')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 11:40','20160826 11:45')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 11:40','20160826 11:45')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 11:40','20160826 11:45')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 11:40','20160826 11:45')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 11:45','20160826 11:50')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 11:45','20160826 11:50')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 11:45','20160826 11:50')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 11:45','20160826 11:50')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 11:45','20160826 11:50')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 11:50','20160826 11:55')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 11:50','20160826 11:55')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 11:50','20160826 11:55')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 11:50','20160826 11:55')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 11:50','20160826 11:55')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 11:50','20160826 11:55')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 11:55','20160826 12:00')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 11:55','20160826 12:00')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 11:55','20160826 12:00')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 11:55','20160826 12:00')
Insert into #sessions(Username, StartTime, EndTime) values ('10000012','20160826 11:55','20160826 12:00')
Insert into #sessions(Username, StartTime, EndTime) values ('10000014','20160826 11:55','20160826 12:00')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 12:00','20160826 12:05')
Insert into #sessions(Username, StartTime, EndTime) values ('10000003','20160826 12:00','20160826 12:05')
Insert into #sessions(Username, StartTime, EndTime) values ('10000004','20160826 12:00','20160826 12:05')
Insert into #sessions(Username, StartTime, EndTime) values ('10000006','20160826 12:00','20160826 12:05')
Insert into #sessions(Username, StartTime, EndTime) values ('10000002','20160826 12:05','20160826 12:10')

-- solution
;With T1 As --join of starttimes with endtimes
(
  Select UserName, StartTime Time
  From #Sessions 

  Union All 

  Select UserName, EndTime 
  From #Sessions
), 
T2 As --remove the duplicate times for each user and number the rows for each user
(
SELECT UserName,Time, Row_Number() Over(Partition By UserName Order By Time) Nm 
FROM T1
GROUP BY UserName,Time
HAVING COUNT(*) = 1
)
--join pairs of two rows for the same user together
Select	
    A.UserName, A.Time as StartTime, B.Time as EndTime
FROM 
    T2 A
    inner join T2 B on A.UserName = b.UserName and A.Nm = B.Nm-1 and (A.Nm %  2) = 1
Order By UserName, StartTime;

drop table #Sessions

Open in new window


I think it's a bit easier to 'read'. And it might have better performance with a lot of data because there is no 'Exists'.
1
 
Nico BontenbalCommented:
Create a cursor based on this query but sort the data on userid and starttime.
Create a temp table (or table variable) with userid, starttime, endtime.
Loop through the cursor and check if there is a record in the temp table for the user with an end time within 5 minutes of the endtime of the record in the cursor. If so update the end time of the existing record in the temp table. If not add a new record to the temp table.
Finally return all the data from the temp table.

If you never worked with cursors before you can start here:
https://msdn.microsoft.com/en-us/library/ms180169.aspx?f=255&MSPPError=-2147217396

If you never worked with temp tables you can start here:
https://www.simple-talk.com/sql/t-sql-programming/temporary-tables-in-sql-server/
0
 
deanmachine333Author Commented:
Thanks Nico ,

but i have found a solution that works for me --

-- solution
With T1 As
(
  Select UserName, StartTime Time
  From Sessions 

  Union All 

  Select UserName, EndTime 
  From Sessions
), 
T2 As 
(
  Select Row_Number() Over(Partition By UserName Order By Time) Nm, 
		UserName, Time 
  From	T1
), 
T3 As 
(
  Select	A.Nm-Row_Number() Over(Partition By A.UserName Order By A.Time,B.Time) Nm1, 
		A.UserName, 
		A.Time StartTime, 
		B.Time EndTime 
  From	T2 A 
    Inner join T2 B 
	   	 On A.UserName = B.UserName 
	  	And A.Nm=B.Nm - 1 
  Where	Exists 
    ( Select	* 
		  From	Sessions S 
		  Where	S.UserName = A.UserName 
		  	And (S.StartTime  = A.Time) ) 
	 	Or A.Time=B.Time
)
Select	UserName, 
		Min(StartTime) StartTime, 
		Max(EndTime) EndTime 
From	T3 
Group By UserName, Nm1 
Order By UserName, StartTime;

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.