hive确定日期重叠的记录

brqmpdu1  于 2021-05-27  发布在  Hadoop
关注(0)|答案(1)|浏览(283)

我有如下表格,需要根据事务id找到重叠的日期
同一事务id的多个记录,需要查找是否有任何重叠的日期并返回这些记录

guz6ccqo

guz6ccqo1#

你可以试试 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 如果可能的话。

相关问题