sql - 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 行)。
解决方案
我认为您想检查重叠期间以确保没有重叠:
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 行,因为这些行没有重叠。
推荐阅读
- java - 如何更改服务运行通知标题、图像和描述?
- node.js - 客户端不显示Flash消息,如何解决?
- python-3.x - Conda 环境包括来自不同环境的包
- javascript - 部署到 Heroku 失败(React/Node monorepo)
- vue.js - fusioncharts在重新加载时出现渲染错误
- c++ - 隐式 const/nonconst 运算符 bool
- hpc - qsub -t 工作“已死”
- flutter - MissingPluginException(在通道 plugins.flutter.io/image_picker 上找不到方法 pickImage 的实现)
- docker - 在 Jenkinsfile 中标记 docker 图像
- microsoft-graph-api - Azure:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:找不到有效的证书路径 toRequestedTarget