score:3

Accepted answer

Using below sample data:

select * into #t from (
select 1 ID, '2016-04-06 09:00:00' r_start , '2016-04-06 09:15:00' r_end union
select 2, '2016-04-06 11:00:00', '2016-04-06 11:30:00'union
select 3, '2016-04-06 12:00:00', '2016-04-06 12:45:00'union
select 4, '2016-04-06 16:30:00', '2016-04-06 16:45:00') AS bookdata

following query, results all the free time slots:

;with booked as (
    select r_start, r_end
    , LAG(r_end) over (order by r_end) PrevBook
    , LEAD(r_start) over (order by r_start) NextBook
    from #t
)
select IsNull(PrevBook, '2016-04-06 07:00:00') AS FreeStart, r_start AS FreeEnd
from booked
union
select r_end, IsNull(NextBook, '2016-04-06 19:00:00')
from booked

Result

+---------------------+---------------------+
|      FreeStart      |       FreeEnd       |
+---------------------+---------------------+
| 2016-04-06 07:00:00 | 2016-04-06 09:00:00 |
| 2016-04-06 09:15:00 | 2016-04-06 11:00:00 |
| 2016-04-06 11:30:00 | 2016-04-06 12:00:00 |
| 2016-04-06 12:45:00 | 2016-04-06 16:30:00 |
| 2016-04-06 16:45:00 | 2016-04-06 19:00:00 |
+---------------------+---------------------+

If you are using SQL older than 2012 you won't have LEAD and LAG so you can use below query instead, assuming your have ID as primary key

;with booked as (
    select r_start, r_end
    , (select top 1 r_end from #t where ID < tbl.ID order by ID desc) PrevBook
    , (select top 1 r_start from #t where ID > tbl.ID order by ID) NextBook
    from #t tbl
)
select IsNull(PrevBook, '2016-04-06 07:00:00') AS FreeStart, r_start AS FreeEnd
from booked
union
select r_end, IsNull(NextBook, '2016-04-06 19:00:00')
from booked

** Please "Mark as Answer" if this post has answered the question

score:1

this works for the data you provided

    select * into #t from (

select '2016-04-06 09:00:00'r_start , '2016-04-06 09:15:00'r_end union all 
select'2016-04-06 11:00:00', '2016-04-06 11:30:00'union all 
select'2016-04-06 12:00:00', '2016-04-06 12:45:00'union all 
select'2016-04-06 16:30:00', '2016-04-06 16:45:00') x

--returns duration between 1st reservation and the next   
select  
datediff(MINUTE,x.r_end,y.r_start)/60 'hours'
,datediff(MINUTE,x.r_end,y.r_start) - datediff(MINUTE,x.r_end,y.r_start)/60 * 60 'minutes'
,x.r_start
,x.r_end
from
(select r_start ,r_end
,row_number() over (order by r_start asc) rowid
from #t) x
left join (select r_start ,r_end
,row_number() over (order by r_start asc) rowid
from #t) y
on x.rowid = y.rowid - 1


--returns unreserved dates 
select  
x.r_end available_from
,y.r_start available_to
from
(select r_start ,r_end
,row_number() over (order by r_start asc) rowid
from #t) x
left join (select r_start ,r_end
,row_number() over (order by r_start asc) rowid
from #t) y
on x.rowid = y.rowid - 1

More questions

More questions with similar tag