SQL generate free slots on the fly from bookings table?

Viewed 402

I know there's a lot of booking questions on php section. Belive me when i tell you that I tried most of them, at least those which seems compatible. So let me explain.

I have this appointments table

 ID |    day     | start | end   | 
 ----------------------------------
  1 | 01-01-2018 | 09:00 | 10:00 | 
  2 | 01-01-2018 | 10:00 | 13:00 | 
  3 | 02-01-2018 | 12:00 | 15:00 | 
  4 | 02-01-2018 | 18:00 | 19:30 | 

I was wondering, it possibile with sql to get empty time slots? The result should like something like:

    day     | start | end
 ---------------------------
 01-01-2018 | 00:00 | 09:00
 01-01-2018 | 13:00 | 23:59
 02-01-2018 | 00:00 | 12:00
 02-01-2018 | 15:00 | 18:00
 02-01-2018 | 19:30 | 23:59

The query should contain 2 dates: start_day + end_day

I prepared a fiddle here: https://www.db-fiddle.com/f/6dm8q8UtmDkkkjExYfMEbx/1

2 Answers

MSSQL Version

WITH X AS 
(
SELECT ROW_NUMBER() OVER (ORDER BY Day, Start)sq, [Day], [Start], [End]
FROM (
SELECT [Day], [Start], [End]
FROM [appointments]
UNION
SELECT Day, '00:00', '00:00'
FROM [appointments]
UNION
SELECT Day, '23:59', '23:59'
FROM [appointments]
) T1
)
SELECT A.Day, A.[End] AS Start, b.[Start] AS End
FROM x A
JOIN x B
ON A.sq = B.sq -1
AND A.[Day] = B.[Day]
AND A.[End] <> b.[Start]

Mysql 5.7 version

SET @RowNumber = 0;

CREATE TABLE cte

SELECT (@RowNumber := @RowNumber+1) AS Rownumber, Day, Start, End
  FROM (
SELECT Day, Start, End
FROM booking
UNION
SELECT Day, '00:00', '00:00'
FROM booking
UNION
SELECT Day, '23:59', '23:59'
FROM booking
) T1
  ORDER BY day ASC, Start ASC

      ;

SELECT A.Day, A.End AS Start, B.Start AS End
FROM cte A
JOIN cte B
ON A.Rownumber = B.Rownumber -1
AND A.Day = B.Day
AND A.End <> B.Start
ORDER BY A.Day asc, A.End asc

Will add a fiddle to demonstrate https://www.db-fiddle.com/f/6dm8q8UtmDkkkjExYfMEbx/2

Mysql 5.7 including days without bookings

SET @RowNumber = 0;

CREATE TABLE cte

SELECT (@RowNumber := @RowNumber+1) AS Rownumber, Day, Start, End
  FROM (
SELECT Day, Start, End
FROM booking
UNION
SELECT Day, '00:00', '00:00'
FROM booking
UNION
SELECT Day, '23:59', '23:59'
FROM booking
) T1
  ORDER BY day ASC, Start ASC

      ;

SELECT DAY, Start, End 
FROM(
SELECT A.Day, A.End AS Start, B.Start AS End
FROM cte A
JOIN cte B
ON A.Rownumber = B.Rownumber -1
AND A.Day = B.Day
AND A.End <> B.Start
UNION
SELECT DATE_ADD(A.Day, INTERVAL 1 DAY) AS Day, B.Start AS Start, A.End AS End
FROM cte A
JOIN cte B
ON A.Rownumber = B.Rownumber -1
AND A.Day <> B.Day
)Result
ORDER BY Day ASC, Start ASC

https://www.db-fiddle.com/f/6dm8q8UtmDkkkjExYfMEbx/3

Here is a solution below. Assumed the logic of ID column is identity(1,1), otherwise please generate row_number() for each column first. Syntax might not match with MySql, but you will get the logic and apply same if u like.

CREATE TABLE #result (
 [day] date NOT NULL,
 [start] time NOT NULL,
 [end] time NOT NULL
) 

declare @maxid int = (select max(id) from #booking), @counter int = 1, @day date
declare @tempStart time = '00:00', @currentStart time ='00:00'
declare @tempDay date = (select TOP 1 [day] from #booking)

while @counter <= @maxid
begin
   set @currentStart = (select start from #booking where id=@counter)
   set @day = (select [day] from #booking where id=@counter)

   if (@day > DATEADD(day,1,@tempDay))
   begin
      insert into #result values (DATEADD(day,1,@tempDay),'00:00', '23:00')
      set @tempDay = @day
   end

   if(@tempStart < @currentStart)
   begin
      insert into #result values (@day, @tempStart, @currentStart)
   end

   if(@counter = @maxid and @tempStart <> '23:59')
   begin
      insert into #result values (@day, (select [end] from #booking where id=@counter), '23:59')
   end

   set @tempStart = (select [end] from #booking where id=@counter)
   set @counter = @counter + 1
end

select * from #result

Example for SQL Server: https://rextester.com/live/VBNJ34000

Related