Solved

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

Posted on 2016-08-30
3
67 Views
Last Modified: 2016-08-31
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
0
Comment
Question by:deanmachine333
  • 2
3 Comments
 
LVL 22

Expert Comment

by:Nico Bontenbal
ID: 41777686
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
 

Author Comment

by:deanmachine333
ID: 41777773
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
 
LVL 22

Accepted Solution

by:
Nico Bontenbal earned 500 total points
ID: 41777890
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question