sql - 查找重复预订
问题描述
我有两个表,当通过表 1 中的预订 ID 和表 2 中的 ID 链接时
所以
Table 1 contains BookingID, MemberID, BookedDate.
Table 2 contains ID, StartDateTime, EndDatetime
我试图标记同一成员 ID 在一个时间范围内(StartDateTime 和 EndDateTime)预订了两次的双重预订。然后删除具有最新 BookedDate 的预订。
有人可以为此提供一个 sql 脚本吗?
希望这是有道理的。
提前致谢
伊恩
解决方案
如果我理解正确,您需要第一个表中的成员 ID,然后您只是在寻找重叠:
with t as (
select t2.*, t1.memberid
from table2 t2 join
table1 t1
on t2.id = t1.bookid
)
select t.*
from t
where exists (select 1
from t t2
where t2.memberid = t.memberid and
t2.id <> t.id and
t2.startdate < t.enddate and
t2.enddate > t.startdate
);
推荐阅读
- javascript - Next.Js 中使用 HOC 的持久布局
- powerbi - DAX 中 TSQL string_split 的使用
- python - 网页抓取 youtube 页面
- excel - VBA EXCEL VLOOKUP .SpecialCells(xlCellTypeVisible) 参数
- java - 对于这些关于 java volatile 和 reordering 的代码,这种理解是否正确?
- mongodb - /api/ - 整个数据库可见?(蒙古数据库)
- gitlab - Gitlab - 从部署中分离 CI
- powershell - 连接 Microsoft 合作伙伴中心 (POWERSHELL)
- java - Android:如何防止应用程序进入后台?
- netlogo - netlogo 中的百分比