首页 > 解决方案 > SQL oracle 检查可用日期

问题描述

我需要查询以检查不冲突的日期(例如请求假期)。

从表 1 中选择所有,其中输入参数是新草稿行中的 date_from 和 date_to。

表一(同事要求):

Select '1' as id, to_date('07.05.2021') as date_from,to_date('14.05.2021') as date_to, '-' as yes_no from dual
union
Select '2' as id, to_date('11.05.2021') as date_from,to_date('18.05.2021') as date_to, 'No' as yes_no from dual
union
Select '3' as id, to_date('10.05.2021') as date_from,to_date('15.05.2021') as date_to, '-' as yes_no from dual
union
Select '4' as id, to_date('01.05.2021') as date_from,to_date('04.05.2021') as date_to, 'No' as yes_no from dual

新草稿行(我的要求):

Select to_date('05.05.2021') as date_from,to_date('10.05.2021') as date_to, 'New' as yes_no from dual

如何从表 1 中获取新行没有冲突的所有行。在我的示例中(第 1 行和第 3 行)。

标签: sqloracle

解决方案


我认为您想检查重叠期间以确保没有重叠:

select t2.*, t1.*
from table1 t1 join
     table2 t2
     on t1.date_to < t23.date_from or
        t1.date_from > t2.date_to;

也就是说,如果第二个表在一行开始之前结束或在一行结束之后开始,则没有重叠。

是一个 db<>fiddle。这将返回第 2 行和第 4 行,因为这些行没有重叠。


推荐阅读