Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-08-30
3
Medium Priority
?
105 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

722 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