3

I have table which has start and end dates, such as below

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)

which gives me this initial table:

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

Problem: What I'm trying to achieve is to get 3 intervals out of this 2 overlapping dates. How to achieve that?

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

gives me

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  

I'm not sure how to continue from here.

What I want to achieve is the following table where the overlaps are seperated.

Expected Result:

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
sertsedat
  • 3,490
  • 1
  • 25
  • 45

2 Answers2

4

One way will be to unpivot the data, then order the dates and then again pivot it excluding some of the records.

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
    )

It may need to be changed a little depending on your real data, but with the current one, it seems to work.

enter image description here

sertsedat
  • 3,490
  • 1
  • 25
  • 45
gotqn
  • 42,737
  • 46
  • 157
  • 243
1

It's an ugly beast, I must admit. And the given answer is 100% better. But still, this does give you the desired output:

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
SQL_M
  • 2,455
  • 2
  • 16
  • 30