【问题标题】:way to check if two intervals overlap in amazon Athena / Presto检查亚马逊Athena / Presto中两个间隔是否重叠的方法
【发布时间】:2022-04-26 22:49:54
【问题描述】:

我想知道我们是否有办法检查亚马逊 athena 中的两个日期是否重叠(在编写 athena 查询时)。我可以在 R/Python 中使用 int_overlaps 和 interval 函数来做到这一点。 例如下面 int_overlaps(interval(LeadStart, LeadEnd), interval(MinStartDate, MaxEndDate)))

Min、Max、Lead 只是应用于数据帧中列的标准 R 函数e.g Minstartdate

我想在 amazon athena 中重复相同的过程,我有两个日期间隔,并检查它们是否重叠,如果它们重叠,我希望在新列中得到 1 或 0 的布尔结果(如 mutate R 中的函数,如果日期重叠或不重叠,则放置 1 或 0)

谢谢 哈里斯

【问题讨论】:

    标签: sql amazon-athena presto


    【解决方案1】:

    假设 LeadStart <= LeadEndMinStartDate <= MaxEndDate,检查日期重叠的 SQL 表达式将是:

    NOT (LeadEnd < MinStartDate OR MaxEndDate < LeadStart)
    

    即英文:NOT(第一次结束在第二次开始之前或第二次结束在第一次开始之前)。

    等价表达式(随你喜欢):

    LeadEnd >= MinStartDate AND MaxEndDate >= LeadStart
    

    【讨论】:

      【解决方案2】:

      如果您有两个时间间隔以及时间间隔 (1) 和时间间隔 (2) 的相应开始和结束时间,并且您想检查它们之间是否存在重叠(在 Athena Presto 中):

      with t0 as (
      select *
      from (
      values
           ('02:00:00', '08:00:00', '01:00:00', '03:00:00'
            ), 
           ('02:00:00', '08:00:00', '03:00:00', '05:00:00'
            ), 
           ('02:00:00', '08:00:00', '05:00:00', '10:00:00'
            ), 
           ('02:00:00', '08:00:00', '10:00:00', '15:00:00'
            ), 
           ('02:00:00', '08:00:00', '01:00:00', '10:00:00'
            ), 
           ('02:00:00', '08:00:00', '00:00:00', '01:00:00'
            )
      ) AS t0 (start_time_1, end_time_1, start_time_2, end_time_2)
      )
      
      select * , 
      case when (start_time_2 <= start_time_1 and end_time_2 >= start_time_1) 
           then 1
           when (start_time_2 >= start_time_1 and start_time_2 <= end_time_1) 
           then 1
           else 0 
           end as time_overlap
      from t0
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多