基于selection-c#和sql的半小时和一小时时段预订

时间:2015-08-05 14:53:13

标签: c# sql-server time availability

我无法在选定的日期找到特定治疗的半小时和一小时时间的可用时间。 用户可以选择在预订结束时选择HALF-an-hour和One-hour 示例=在选定日期 - 预订上午9点1小时(上午9点至上午10点) 还有另一个预订 - 上午11点半小时(上午11点到11点30分) 那么用户不应该在同一天选择这两个插槽 他应该在显示屏上显示(选择处理者和日期后)

半小时:

  • 上午9点至9点30分❌(不可用)
  • 上午9:30至上午10点❌(不可用)
  • 上午10点至10点30分(可用)
  • 1030至11am✅(可用)
  • 上午11点至11点30分(不可用)
  • 11:30-12:00✅(可用) 等等...................

    一小时

  • 上午9点至上午10点❌(不可用)

  • 上午10点到11点✅(可用)
  • 上午11点到下午12点❌(不可用),((((如果可能,我们可以在上午11点30分到下午12点30分✅(可用),然后从12点30分开始继续等等......)) )
  • 中午12点至下午1点✅(可用)
  • 下午1点到2点✅(可用) 等等---------------------------

    我试着这样做。

我创建了两个表 - 一个用于HALF-a-hour小时插槽,另一个用于一小时插槽。

这两个表有timebegin和timeEnd

Half-an-hour slots table One-Hour time slots table

我有另一张表,其中包含已预订的条目。 enter image description here

我尝试在SQl中使用EXCEPT - 但这似乎给出了错误的结果



	SELECT T1.timeBegin from ClinicNew.HalfTiming T1 
		left join  ClinicNew.FullTiming T2
		On T1.TimeBegin=T2.TimeBegin
		EXCEPT
		select distinct T1.timeBegin from ClinicNew.HalfTiming T1 
		inner join ClinicNew.NewTreaterEngagedDTM T2
		On T1.timeBegin = T2.timeBegin
		where T2.BookedDate = '2014-04-15'
		and T2.TreaterID=




请帮忙

1 个答案:

答案 0 :(得分:1)

我认为通过为不同长度的时隙提供多个表,您可能会过度复杂化。如果你想以15分钟而不是30分钟的间隔发生什么?想要允许90分钟的预约会发生什么?如果安排这些约会的办公室在不同的日子有不同的工作时间会怎样?

我在下面提出的解决方案使用一个表来存储约会,就是这样。显示的其余逻辑可以很容易地进入存储过程或您想要给定日期的可用约会列表时调用的内容。希望这些评论足以解释正在发生的事情。

-- Sample data from the question.
declare @Appointment table
(
    [ID] bigint not null identity(1, 1), -- Primary key.
    [BookedDate] date not null,          -- The date of the appointment.
    [Time] time(0) not null,             -- The start time of the appointment.
    [Duration] int not null              -- The length of the appointment in minutes.
);
insert @Appointment
    ([BookedDate], [Time], [Duration])
values
    ('2014-04-15', '09:00', 60),
    ('2014-04-15', '10:00', 30),
    ('2014-04-15', '17:00', 60),
    ('2014-04-15', '18:30', 30);

-- @StartTime is the time the office opens on the desired date.
-- @EndTime is the time the office closes on the desired date.
-- @Interval is the number of minutes that separate potential appointment times.
-- @DesiredDate is the date on which an appointment is requested.
-- @DesiredLength is the length of the requested appointment in minutes.
declare @StartTime time(0) = '09:00';
declare @EndTime time(0) = '21:00';
declare @Interval int = 30;
declare @DesiredDate date = '2014-04-15';
declare @DesiredLength int = 30;

-- This CTE enumerates all potential timeslots on the @DesiredDate given the above data.
with [TimeSlotCTE] as
(
    -- Base case: the first appointment slot of the day.
    select 
        [From] = @StartTime, 
        [To] = dateadd(minute, @DesiredLength, @StartTime)

    union all

    -- Recursive case: create a subsequent appointment slot as long as doing so won't
    -- take us past the office's closing time.
    select
        dateadd(minute, @Interval, [From]),
        dateadd(minute, @Interval, [To])
    from
        [TimeSlotCTE]
    where
        dateadd(minute, @Interval, [To]) <= @EndTime
)

-- Finally, we simply select every time slot defined above for which there does not
-- yet exist an overlapping appointment on the requested date.
select
    [T].[From],
    [T].[To],
    [Available] = 
        case when exists 
        (
            select 1 from @Appointment [A]
            where
                -- Forgot this line the first time around!
                [A].[BookedDate] = @DesiredDate and
                [A].[Time] < [T].[To] and
                dateadd(minute, [A].[Duration], [A].[Time]) > [T].[From]
        )
        then 'No' else 'Yes' end
from
    [TimeSlotCTE] [T];

如果我使用@DesiredLength = 30运行上述代码,则输出如下:

From        To          Available
09:00:00    09:30:00    No
09:30:00    10:00:00    No
10:00:00    10:30:00    No
10:30:00    11:00:00    Yes
11:00:00    11:30:00    Yes
11:30:00    12:00:00    Yes
12:00:00    12:30:00    Yes
12:30:00    13:00:00    Yes
13:00:00    13:30:00    Yes
13:30:00    14:00:00    Yes
14:00:00    14:30:00    Yes
14:30:00    15:00:00    Yes
15:00:00    15:30:00    Yes
15:30:00    16:00:00    Yes
16:00:00    16:30:00    Yes
16:30:00    17:00:00    Yes
17:00:00    17:30:00    No
17:30:00    18:00:00    No
18:00:00    18:30:00    Yes
18:30:00    19:00:00    No
19:00:00    19:30:00    Yes
19:30:00    20:00:00    Yes
20:00:00    20:30:00    Yes
20:30:00    21:00:00    Yes

这是@DesiredLength = 60

From        To          Available
09:00:00    10:00:00    No
09:30:00    10:30:00    No
10:00:00    11:00:00    No
10:30:00    11:30:00    Yes
11:00:00    12:00:00    Yes
11:30:00    12:30:00    Yes
12:00:00    13:00:00    Yes
12:30:00    13:30:00    Yes
13:00:00    14:00:00    Yes
13:30:00    14:30:00    Yes
14:00:00    15:00:00    Yes
14:30:00    15:30:00    Yes
15:00:00    16:00:00    Yes
15:30:00    16:30:00    Yes
16:00:00    17:00:00    Yes
16:30:00    17:30:00    No
17:00:00    18:00:00    No
17:30:00    18:30:00    No
18:00:00    19:00:00    No
18:30:00    19:30:00    No
19:00:00    20:00:00    Yes
19:30:00    20:30:00    Yes
20:00:00    21:00:00    Yes

这样的事情对你有用吗?