如何从重叠的日期中获得单独的间隔

时间:2018-12-07 09:28:25

标签: sql sql-server tsql date overlap

我有一个表,其中包含开始日期和结束日期,例如以下

drop table if exists #Temp
select * 
into #Temp
from (values (1, 1, '2011-10-09', '2011-10-20'),
             (2, 1, '2011-10-14', '2011-10-30'),
             (3, 1, '2012-05-21', '2012-05-25')
     ) as value (id, userId, d1, d2)

这给了我这个初始表:

id  userId    d1            d2
1   1         2011-10-09    2011-10-20
2   1         2011-10-14    2011-10-30
3   1         2012-05-21    2012-05-25

问题:我要解决的是从这2个重叠的日期中获得3个间隔。如何实现?

select * 
from 
    #Temp t1
    join #Temp t2
        on t1.userId = t2.userId
        and t1.id != t2.id
where
    t1.d1 <= t2.d2
    and t1.d2 >= t2.d1
order by t1.d1

给我

id  userId    d1            d2              id  userId    d1            d2
1   1         2011-10-09    2011-10-20      2   1         2011-10-14    2011-10-30
2   1         2011-10-14    2011-10-30      1   1         2011-10-09    2011-10-20  

我不确定如何从这里继续。

我要实现的是下表,其中重叠部分是分开的。

预期结果:

userId  d1          d2              
1       2011-10-09  2011-10-14
1       2011-10-14  2011-10-20
1       2011-10-20  2011-10-30
1       2012-05-21  2012-05-25

2 个答案:

答案 0 :(得分:4)

一种方法是取消数据透视,然后对日期排序,然后再对数据透视,排除某些记录。

WITH dataSource AS
(
    select id, userId, d1
    from #Temp t1
    UNION ALL
    select id, userId, d2
    from #Temp t1
), DataSourceOrdered AS
(
    SELECT 
        id, userId, d1,
        row_number () OVER (PARTITION BY userId ORDER By d1) as rowid
    FROM dataSource DS1
)
SELECT *
FROM DataSourceOrdered ds1
INNER JOIN DataSourceOrdered ds2
    ON ds1.userId = ds2.[userId]
    and ds1.[rowid] + 1 = ds2.[rowid]
    and exists (
        SELECT 1 
        FROM #Temp x 
        WHERE ds1.[d1] >= x.d1 and ds1.[d1] < x.d2 and ds1.userId = x.userId
    )

可能需要根据实际数据进行一些更改,但是对于当前数据,它似乎可以正常工作。

enter image description here

答案 1 :(得分:1)

我必须承认,这是丑陋的野兽。给出的答案要好100%。但是,这确实可以为您提供所需的输出:

SELECT * 
INTO #Temp
FROM (VALUES (1, 1, '2011-10-09', '2011-10-20'),
             (2, 1, '2011-10-14', '2011-10-30'),
             (3, 1, '2012-05-21', '2012-05-25')
     ) AS VALUE (id, userId, d1, d2)

WITH CTE AS 
(
    SELECT *, ROW_NUMBER() OVER (ORDER BY d1, d2) AS RowNumb
    FROM #Temp
)

SELECT C.*, 
      C2.d1 AS C2D1,
      C2.D2 AS C2D2,
      CASE WHEN CAST (C2.d1 AS DATETIME ) < CAST (C.d2 AS DATETIME) THEN 1 ELSE 0 END AS InsertField 
INTO #NewTable
FROM CTE AS C
LEFT JOIN CTE AS C2 ON C.RowNumb = C2.RowNumb - 1

SELECT * FROM #NewTable

SELECT id, userId, d1, CASE WHEN CAST (d2 AS DATETIME) < CAST (ISNULL (C2D1, '29000101') AS DATETIME) THEN d2 ELSE C2D1 END AS d2
INTO #Results
FROM #NewTable
UNION ALL
SELECT id, userid, C2D1, d2
FROM #NewTable
WHERE InsertField = 1

SELECT * FROM #NewTable

SELECT *, ROW_NUMBER() OVER (ORDER BY d1, d2) AS RowNumb 
INTO #R
FROM #Results AS R

SELECT R.id,
      R.userId,
      CASE WHEN CAST (R.d1 AS DATETIME) < CAST (R2.d2 AS DATETIME) THEN R2.d2 ELSE R.d1 END AS D1,
      R.d2
FROM #R AS R
LEFT JOIN #R AS R2 ON R.RowNumb = R2.RowNumb + 1