oracle查询两个时间段是否有交集,解决预约时间碰撞问题

抛出问题

有一设备资源,某些时间段已经被预约,现在创建新的预约单,需要判断本次预约的时间是否有碰撞冲突?

1.准备数据

create table TABLE_EQ_APPOINTMENT
(
  eq_id                VARCHAR2(30),
  appointmentStartTime DATE,
  appointmentEndTime   DATE
)
;
-- Add comments to the columns 
comment on column TABLE_EQ_APPOINTMENT.eq_id
  is '设备资源';
comment on column TABLE_EQ_APPOINTMENT.appointmentStartTime 
  is '预约开始时间';
comment on column TABLE_EQ_APPOINTMENT.appointmentEndTime   
  is '预约结束时间';

insert into table_eq_appointment (EQ_ID, APPOINTMENTSTARTTIME, APPOINTMENTENDTIME)
values ('01E03614', to_date('31-03-2022 09:00:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('31-03-2022 18:00:00', 'dd-mm-yyyy hh24:mi:ss'));

insert into table_eq_appointment (EQ_ID, APPOINTMENTSTARTTIME, APPOINTMENTENDTIME)
values ('01E03614', to_date('30-03-2022 09:00:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('30-03-2022 18:00:00', 'dd-mm-yyyy hh24:mi:ss'));

insert into table_eq_appointment (EQ_ID, APPOINTMENTSTARTTIME, APPOINTMENTENDTIME)
values ('01E03614', to_date('31-03-2022 18:00:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('31-03-2022 19:00:00', 'dd-mm-yyyy hh24:mi:ss'));

insert into table_eq_appointment (EQ_ID, APPOINTMENTSTARTTIME, APPOINTMENTENDTIME)
values ('01E03614', to_date('04-04-2022 10:00:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('07-04-2022 19:00:00', 'dd-mm-yyyy hh24:mi:ss'));

insert into table_eq_appointment (EQ_ID, APPOINTMENTSTARTTIME, APPOINTMENTENDTIME)
values ('01E03614', to_date('02-04-2022 10:00:00', 'dd-mm-yyyy hh24:mi:ss'), to_date('02-04-2022 11:00:00', 'dd-mm-yyyy hh24:mi:ss'));

查询sql:

   select
        u.*
        from table_eq_appointment u  where
        u.EQ_ID = '01E03614'
        and  u.appointmentendtime >to_date('2022-04-02'and  u.appointmentstarttime <to_date('2022-04-07'

1.全量数据
在这里插入图片描述
2.查询碰撞时间数据 (开始时间 start 结束时间 end)
在这里插入图片描述

方法一:根据碰撞的三种情况

1.在已预约时间前面    start<appointmentStartTime<end<appointmentEndTime
2.在已预约时间内    appointmentStartTime<start<end<appointmentEndTime
3.在已预约时间后面    appointmentStartTime<start<appointmentEndTime<end

SELECT * FROM table_eq_appointment 
WHERE
    (appointmentStartTime>= start  AND appointmentEndTime<= end)
    OR (appointmentStartTime<= start    AND appointmentEndTime>= end)
    OR (appointmentStartTime>= start    AND appointmentEndTime<= end)

方法二:根据不碰撞的两种情况,查询非不碰撞时间,即碰撞

1.在已预约时间前面    start<end<appointmentStartTime<appointmentEndTime
2.在已预约时间后面    appointmentStartTime<appointmentEndTime<start<end

SELECT * FROM table_eq_appointment u
WHERE NOT (u.appointmentendtime <start or u.appointmentstarttime >end)

方法三:根据碰撞移动

在这里插入图片描述

1.在已预约时间前面    start<appointmentStartTime<end<appointmentEndTime
2.在已预约时间内    appointmentStartTime<start<end<appointmentEndTime
3.在已预约时间后面    appointmentStartTime<start<appointmentEndTime<end

三种情况组合起来就是,需要同时满足:
appointmentstarttime < end  and start<appointmentEndTime

SELECT * FROM table_eq_appointment u
WHERE  u.appointmentendtime >start and u.appointmentstarttime < end
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值