1

選択した日付の特定のトリートメントの 1 時間半と 1 時間の時間枠の利用可能時間を見つけるのに苦労しています。ユーザーは、予約終了時に 1 時間半と 1 時間を選択するオプションがあります 例 = 選択した日付 - 午前 9 時に 1 時間 (午前 9 時から午前 10 時) の予約があり、別の予約があります - 午前 11 時の半時間です。 -hour (午前 11 時から午前 11 時 30 分) の場合、ユーザーは、選択した同じ日にこれらの 2 つのスロットを表示する必要はありません。ユーザーは、これをディスプレイに表示する必要があります (治療者と日付を選択した後)。

30分:

  • 午前9時から午前9時30分❌(利用不可)
  • 午前9時30分から午前10時❌(利用不可)
  • 午前 10 時 ~ 10 時 30 分まで ✅ (利用可能)
  • 10:30 から午前 11 時まで ✅ (利用可能)
  • 11:00~11:30 ❌(利用不可)
  • 11:30~12:00 ✅(空きあり)など.................... .

    一時間

  • 午前9時から午前10時❌(利用不可)

  • 午前10時から午前11時まで✅(利用可能)
  • 午前 11 時から午後 12 時 ❌ (利用不可), ((((ここで、可能であれば、午前 11 時 30 分から午後 12 時 30 分まで作成できます ✅ (利用可能)、その後、シーケンスは 12:30 から続きます....)))
  • 12pm~1pm ✅ (利用可能)
  • 午後1時~午後2時 ✅(空きあり)など-----------------------------------------

    こんな感じにしてみました。

2 つのテーブルを作成しました。1 つは 1 時間半のスロット用で、もう 1 つは 1 時間のスロット用です。

これらの 2 つのテーブルには timebegin と timeEnd があります

30分枠表 1時間枠表

予約済みのエントリを持つ別のテーブルがあります。 ここに画像の説明を入力

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=

助けてください

4

1 に答える 1

2

異なる長さのタイムスロットに複数のテーブルを用意することで、おそらくこれを過度に複雑にしていると思います。30 分間隔ではなく 15 分間隔にしたい場合はどうなりますか? 90 分の予定を許可するとどうなりますか? これらの予定をスケジュールしているオフィスが、異なる日に異なる時間を設定している場合はどうなりますか?

以下で提案するソリューションでは、1 つのテーブルを使用して予定を保存します。それだけです。示されている残りのロジックは、特定の日付に利用可能な予定のリストが必要なときに呼び出すストアド プロシージャまたは何かに簡単に入れることができます。うまくいけば、コメントは何が起こっているのかを説明するのに十分です.

-- 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

このようなものはあなたのために働くでしょうか?

于 2015-08-05T19:22:29.013 に答える