Solved

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

Posted on 2016-08-30
3
45 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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now