I'm building up a website where you can take an appointement online. I'll not explain all in detail but I have an table with my available time to take an appointment. Distributed into intervals of 5 min. Here's an example:
ID StartDate EndDate
492548 2016-12-16 08:00:00.000 2016-12-16 08:05:00.000
492549 2016-12-16 08:05:00.000 2016-12-16 08:10:00.000
492550 2016-12-16 08:10:00.000 2016-12-16 08:15:00.000
492551 2016-12-16 08:15:00.000 2016-12-16 08:20:00.000
492552 2016-12-16 08:20:00.000 2016-12-16 08:25:00.000
492553 2016-12-16 08:25:00.000 2016-12-16 08:30:00.000
492554 2016-12-16 08:30:00.000 2016-12-16 08:35:00.000
492555 2016-12-16 08:35:00.000 2016-12-16 08:40:00.000
492556 2016-12-16 08:40:00.000 2016-12-16 08:45:00.000
492557 2016-12-16 08:45:00.000 2016-12-16 08:50:00.000
492558 2016-12-16 08:50:00.000 2016-12-16 08:55:00.000
492559 2016-12-16 08:55:00.000 2016-12-16 09:00:00.000
492560 2016-12-16 09:00:00.000 2016-12-16 09:05:00.000
492561 2016-12-16 09:05:00.000 2016-12-16 09:10:00.000
492562 2016-12-16 09:10:00.000 2016-12-16 09:15:00.000
492563 2016-12-16 09:15:00.000 2016-12-16 09:20:00.000
492564 2016-12-16 09:20:00.000 2016-12-16 09:25:00.000
492565 2016-12-16 09:25:00.000 2016-12-16 09:30:00.000
492566 2016-12-16 09:30:00.000 2016-12-16 09:35:00.000
Depending the consultation time, based in the reason for consultation, I have to group these rows into one and know the min(IDSchedulingInterval) and the max(IDSchedulingInterval).
Here's an example of the result I want if I have a duration time of 15 min:
Min(ID) Max(ID) StartDate EndDate
492548 492550 2016-12-16 08:00:00.000 2016-12-16 08:15:00.000
492551 492553 2016-12-16 08:15:00.000 2016-12-16 08:30:00.000
492554 492556 2016-12-16 08:30:00.000 2016-12-16 08:45:00.000
492557 492559 2016-12-16 08:45:00.000 2016-12-16 09:00:00.000
The duration time can change. I don't know how to proceed to make this query..
EDIT Here are some exception you have to check. Here's my table
ID StartDate EndDate Isreserved
492548 2016-12-16 08:00:00.000 2016-12-16 08:05:00.000 0
492549 2016-12-16 08:05:00.000 2016-12-16 08:10:00.000 0
492550 2016-12-16 08:10:00.000 2016-12-16 08:15:00.000 0
492551 2016-12-16 08:15:00.000 2016-12-16 08:20:00.000 0
492552 2016-12-16 08:20:00.000 2016-12-16 08:25:00.000 0
492555 2016-12-16 08:35:00.000 2016-12-16 08:40:00.000 0
492556 2016-12-16 08:40:00.000 2016-12-16 08:45:00.000 0
492557 2016-12-16 08:45:00.000 2016-12-16 08:50:00.000 1
492558 2016-12-16 08:50:00.000 2016-12-16 08:55:00.000 1
492559 2016-12-16 08:55:00.000 2016-12-16 09:00:00.000 1
492560 2016-12-16 09:00:00.000 2016-12-16 09:05:00.000 0
492561 2016-12-16 09:05:00.000 2016-12-16 09:10:00.000 0
492562 2016-12-16 09:10:00.000 2016-12-16 09:15:00.000 0
492563 2016-12-16 09:15:00.000 2016-12-16 09:20:00.000 0
492564 2016-12-16 09:20:00.000 2016-12-16 09:25:00.000 0
492565 2016-12-16 09:25:00.000 2016-12-16 09:30:00.000 0
492566 2016-12-16 09:30:00.000 2016-12-16 09:35:00.000 0
Here the time between 8:45 to 9:00 is reserved so you can't take it. Also you don't have time between 8:25 and 8:35 so you can't reserved it either. An example, if I want to take a appointment of 30 min then I should have a result like this one:
Min(ID) Max(ID) StartDate EndDate
492560 492565 2016-12-16 09:00:00.000 2016-12-16 09:30:00.000
Only 1 row will be returned because you don't have enough time between other intervals
EDIT 2
Thanks to DVT I have modified is query and i'm almost having my query work the only hic here is the overlapping time. here's my query:
DECLARE @newinterval INT = 60;
;with cte as (
SELECT
t1.IdSchedulingByInterval AS IdSchedulingByIntervalMin
, t2.IdSchedulingByInterval AS IdSchedulingByIntervalMax
, t1.SchedulingByIntervalStartDate
, t2.SchedulingByIntervalEndDate
FROM
RDV_tbSchedulingByInterval t1
JOIN RDV_tbSchedulingByInterval t2 ON t2.SchedulingByIntervalStartDate = DATEADD(minute, @newinterval - 5, t1.SchedulingByIntervalStartDate)
) select * from cte where (select SUM(5) from RDV_tbSchedulingByInterval where IdSchedulingByInterval
between cte.IdSchedulingByIntervalMin and cte.IdSchedulingByIntervalMax ) = @newinterval
order by cte.SchedulingByIntervalStartDate
Here's my result:
492551 492562 2016-12-16 08:15:00.000 2016-12-16 09:15:00.000
492552 492563 2016-12-16 08:20:00.000 2016-12-16 09:20:00.000
492553 492564 2016-12-16 08:25:00.000 2016-12-16 09:25:00.000
492554 492565 2016-12-16 08:30:00.000 2016-12-16 09:30:00.000
492555 492566 2016-12-16 08:35:00.000 2016-12-16 09:35:00.000
492556 492567 2016-12-16 08:40:00.000 2016-12-16 09:40:00.000
492557 492568 2016-12-16 08:45:00.000 2016-12-16 09:45:00.000
492558 492569 2016-12-16 08:50:00.000 2016-12-16 09:50:00.000
492559 492570 2016-12-16 08:55:00.000 2016-12-16 09:55:00.000
492560 492571 2016-12-16 09:00:00.000 2016-12-16 10:00:00.000
492561 492572 2016-12-16 09:05:00.000 2016-12-16 10:05:00.000
492562 492573 2016-12-16 09:10:00.000 2016-12-16 10:10:00.000
492563 492574 2016-12-16 09:15:00.000 2016-12-16 10:15:00.000
492564 492575 2016-12-16 09:20:00.000 2016-12-16 10:20:00.000
492565 492576 2016-12-16 09:25:00.000 2016-12-16 10:25:00.000
492566 492577 2016-12-16 09:30:00.000 2016-12-16 10:30:00.000
492567 492578 2016-12-16 09:35:00.000 2016-12-16 10:35:00.000
492568 492579 2016-12-16 09:40:00.000 2016-12-16 10:40:00.000
492569 492580 2016-12-16 09:45:00.000 2016-12-16 10:45:00.000
Expected result:
492551 492562 2016-12-16 08:15:00.000 2016-12-16 09:15:00.000
492563 492574 2016-12-16 09:15:00.000 2016-12-16 10:15:00.000
I don't want time to overlapped other