Solved

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

Posted on 2016-08-30
3
62 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

803 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