本文介绍了时间间隔重叠-Terdata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关间隔超额补充的帮助。我将这些记录放在一个表中(还有更多):

示例1:

Id---------StartDate------EndDate

794122    2011-05-10    2999-12-31

794122    2011-04-15    2999-12-31

794122    2008-04-03    2999-12-31

794122    2008-03-31    2999-12-31

794122    2008-02-29    2999-12-31

794122    2008-02-04    2999-12-31

794122    2007-10-10    2999-12-31

794122    2007-09-15    2999-12-31

示例2:

Id---------StartDate------EndDate

5448    2012-12-28      2999-12-31

5448    2011-06-30      2999-12-31

5448    2005-12-26      2011-06-30

5448    2005-06-15      2011-06-30

5448    2006-07-31      2006-12-31

5448    2001-03-31      2006-07-15

示例3:

Id---------StartDate------EndDate

214577    2007-02-28    2999-12-31

214577    2003-06-20    2007-03-04

214577    2003-06-20    2007-02-28

示例4:

Id---------StartDate-------EndDate

9999    2008-05-28      2999-01-01

9999    2005-03-03      2008-05-31

9999    2005-05-31      2005-12-31

9999    2003-12-01      2005-08-12

9999    2001-01-01      2002-03-05

9999    2000-01-08      2002-01-01

我想要:

*Example1* - 2007-09-15->3000-01-01

*Example2* - 2001-03-31->3000-01-01

*Example3* - 2003-06-20->3000-01-01

*Example4* - 2003-12-01->3000-01-01

你有什么建议我是怎么做的吗?因为我没有选择最大值和最小值(按ID分组)->此问题在示例4中。

谢谢!

推荐答案

示例#4的结果与您的数据不匹配,不是应该是9999,2999-01-02而不是3000-01-01吗?

合并重叠期间的典型解决方案使用嵌套的OLAP函数,针对您的特定需求(仅限最新期间),可以稍微简化为:

SELECT *
FROM
 (
   SELECT DISTINCT -- DISTINCT is not neccessary, but results in a better plan
      Id,
      StartDate,
      MAX(EndDate)
      OVER (PARTITION BY Id) + 1 AS EndDate
   FROM dropme AS t
   QUALIFY -- find the gap
      COALESCE(StartDate
               - MAX(EndDate)
                 OVER (PARTITION BY Id
                       ORDER BY StartDate, EndDate
                       ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING), 1) > 0
 ) AS dt
QUALIFY
   ROW_NUMBER()
   OVER (PARTITION BY Id
         ORDER BY StartDate DESC) = 1
;

这篇关于时间间隔重叠-Terdata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 21:33