首页 > 解决方案 > 查找重复预订

问题描述

我有两个表,当通过表 1 中的预订 ID 和表 2 中的 ID 链接时

所以

Table 1 contains BookingID, MemberID, BookedDate.
Table 2 contains ID, StartDateTime, EndDatetime

我试图标记同一成员 ID 在一个时间范围内(StartDateTime 和 EndDateTime)预订了两次的双重预订。然后删除具有最新 BookedDate 的预订。

有人可以为此提供一个 sql 脚本吗?

希望这是有道理的。

提前致谢

伊恩

标签: sqlsql-server-2008

解决方案


如果我理解正确,您需要第一个表中的成员 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
             );

推荐阅读