sql - 配置单元确定日期重叠的记录

我有如下的tableA,需要根据交易ID查找重叠的日期

同一交易ID的多个记录,需要查找是否有重叠的日期并返回这些记录

最佳答案

您可以尝试exists:

select t.*
from t
where exists (select 1
              from t t2
              where t2.transactionId = t.transactionId and
                    t2.enddate > t.startdate and
                    t2.startdate < t.enddate and
                    -- and not the same record
                    t2.startdate <> t.startdate and
                    t2.enddate <> t.enddate
             );

像这样查询短语怎么样?
select t.*
from t join
     t t2
     on t2.transactionId = t.transactionId 
where t2.enddate > t.startdate and
      t2.startdate < t.enddate and
      t2.startdate <> t.startdate and
      t2.enddate <> t.enddate

(如果您具有唯一的ID,那么最后两个条件可以用该ID代替。)

如果可能,您可能需要select distinct

https://stackoverflow.com/questions/55186971/

相关文章:

docker - docker 容器可以连接到 SRIOV 虚拟功能吗?

hadoop - HDFS如何存储大于 block 大小的单个数据?

dns - 在链接的Docker容器中解析内部DNS

nginx - 在docker中无法将nginx与ghost链接

php - PHP shell_exec无法执行Hadoop命令

hadoop - Hadoop 2.7.7,无法使用端口8088打开Resource Manager

hadoop - reducer 的默认数量

django - 如果我将我的Elastic Beanstalk应用程序移动为使用Docker,是否

hadoop - 从Teradata查询到pyspark

hadoop - 在实际运行oozie工作流之前如何检测错误?