简体   繁体   English

合并具有连续日期范围的记录

[英]Merge records with consecutive date ranges

I am trying to figure out how to combine multiple records together into one where the TERM Date of the first record is the day before the EFFective date in the other record. 我试图弄清楚如何将多个记录组合成一个记录,其中第一个记录的TERM日期是另一个记录中生效日期的前一天。

Here's some sample data: 以下是一些示例数据:

SELECT 1 AS MEM_ID, CAST('2017-01-01' AS DATE) AS EFF_DATE, CAST('2017-05-31' AS DATE) AS TERM_DATE
INTO #TEMP 
UNION 
SELECT 1 AS MEM_ID, CAST('2017-06-01' AS DATE) AS EFF_DATE, CAST('2018-01-31' AS DATE)
UNION 
SELECT 1 AS MEM_ID, CAST('2018-02-01' AS DATE) AS EFF_DATE, CAST('2018-06-30' AS DATE)
UNION 
SELECT 1 AS MEM_ID, CAST('2018-09-01' AS DATE) AS EFF_DATE, CAST('2078-12-31' AS DATE)
UNION 
SELECT 2 AS MEM_ID, CAST('2017-02-01' AS DATE) AS EFF_DATE, CAST('2017-04-30' AS DATE)
UNION 
SELECT 2 AS MEM_ID, CAST('2017-05-01' AS DATE) AS EFF_DATE, CAST('2018-03-31' AS DATE)
UNION 
SELECT 2 AS MEM_ID, CAST('2018-06-01' AS DATE) AS EFF_DATE, CAST('2018-06-30' AS DATE)
UNION 
SELECT 2 AS MEM_ID, CAST('2018-07-01' AS DATE) AS EFF_DATE, CAST('2078-12-31' AS DATE)
UNION 
SELECT 3 AS MEM_ID, CAST('2017-01-01' AS DATE) AS EFF_DATE, CAST('2017-10-31' AS DATE)
UNION 
SELECT 3 AS MEM_ID, CAST('2017-12-01' AS DATE) AS EFF_DATE, CAST('2018-03-31' AS DATE)
UNION 
SELECT 3 AS MEM_ID, CAST('2018-04-01' AS DATE) AS EFF_DATE, CAST('2018-06-30' AS DATE)
UNION 
SELECT 3 AS MEM_ID, CAST('2018-07-01' AS DATE) AS EFF_DATE, CAST('2078-12-31' AS DATE)

I created a CTE that JOINs onto itself by DATE = DATE - 1 of the second record. 我创建了一个CTE,该CTE在第二条记录的DATE = DATE-1之前加入自身。 unfortunately, it only combines two records and I haven't figured out how to make it recursive. 不幸的是,它只合并了两个记录,我还没有弄清楚如何使其递归。

;WITH TEST AS (
SELECT DISTINCT MEM_ID,
    EFF_DATE,
    TERM_DATE, 
    ROW_NUMBER()OVER(ORDER BY MEM_ID, EFF_DATE) ROW_NUM
FROM #TEMP
)

SELECT * 
FROM TEST T 
INNER JOIN TEST T2 ON T.MEM_ID = T2.MEM_ID AND T.TERM_DATE = DATEADD(DAY, -1, T2.EFF_DATE)

The problem with this method is that there can be three or more consecutive records that need to be combined into one. 这种方法的问题在于可能需要将三个或更多连续记录合并为一个。

ID 1 should have two records - one from 1/1/2017 - 6/30/2018 and one from 9/1/2018 - 12/31/78 . ID 1应该有两个记录-一个来自1/1/ 1/1/2017 - 6/30/2018 9/1/2018 - 12/31/78和一个来自9/1/2018 - 12/31/78 1/ 9/1/2018 - 12/31/78

ID 2 should also have two records - 2/1/2017 - 3/30/2018 and 6/1/2018 - 12/31/78 . ID 2还应具有两个记录2/1/2017 - 3/30/2018 / 6/1/2018 - 12/31/786/1/2018 - 12/31/78

ID 3 should also have two records - 1/1/2017 - 10/31/2017 and 12/1/2017 - 12/31/78 . ID 3还应具有两个记录-1/1/ 1/1/2017 - 10/31/2017 / 12/1/2017 - 12/31/7812/1/2017 - 12/31/78 / 1/1/2017 - 10/31/2017 12/1/2017 - 12/31/78

How can I combine these records? 如何合并这些记录? The records will mostly have the first of the month for the start date and the last day of the month for the term date - I don't need to worry about the outliers. 记录将大部分包含开始日期的月份的第一天,以及包含学期日期的月份的最后一天-我不必担心异常值。

I've looked into similar question but they only have two records that need to be combined and use the same method I used. 我研究了类似的问题,但它们只有两条记录需要合并,并使用与我相同的方法。

This is the type of stuff that LAG and LEAD are useful for. 这是LAG和LEAD有用的东西类型。

;WITH CTE AS (SELECT DISTINCT  MEM_ID,
                  EFF_DATE,
                  LEAD(EFF_DATE, 1, NULL) OVER (PARTITION BY MEM_ID ORDER BY MEM_ID, EFF_DATE) AS TERM_DATE
              FROM #TEMP
             )

SELECT MEM_ID,
       EFF_DATE,
       TERM_DATE, 
       DATEADD(DAY, -1, TERM_DATE) AS D2
FROM CTE;

LEAD basically peaks into the next row, and includes the specified column from the peaked into row in this row. LEAD基本上会到达下一行,并包括从该行中达到峰值的指定列。 LAG does the previous row. LAG执行上一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM