Record in multiple entries by start and end date

I am looking for a solution in which I need to create a recordset from one record using data from another table. Table definition:

DECLARE A AS TABLE
(
 AID BIGINT NOT NULL,
 StartDate DATETIME NOT NULL,
 EndDate DATETIME
)

DECLARE B AS TABLE
(
 AID BIGINT NOT NULL,
 StartDate DATETIME NOT NULL,
 EndDate DATETIME NULL
)

The idea is that when A contains:

1 | 01-01-2010 | 01-02-2010
2 | 01-10-2010 | 31-10-2010

and B contains:

1 | 01-01-2010 | 15-01-2010
2 | 15-10-2010 | 20-10-2010

we get 5 entries:

1 | 01-01-2010 | 15-01-2010
1 | 16-01-2010 | 01-02-2010
2 | 01-10-2010 | 15-10-2010
2 | 16-10-2010 | 20-10-2010
2 | 21-10-2010 | 31-10-2010

We are currently doing this with the cursor on A and the cursor of the inner loop on B, we must do this in SQLServer (TSQL or, in the worst case, CLR)

Any ideas on how to write this as select so the cursor overhead disappears?

+3
source share
1 answer
DECLARE @A TABLE (AID BIGINT NOT NULL, StartDate DATETIME NOT NULL, EndDate DATETIME)
DECLARE @B TABLE (AID BIGINT NOT NULL, StartDate DATETIME NOT NULL, EndDate DATETIME NULL)

SET DATEFORMAT dmy
INSERT @A VALUES (1 ,'01-01-2010','01-02-2010')
INSERT @A VALUES (2 ,'01-10-2010','31-10-2010')
INSERT @B VALUES (1 ,'01-01-2010','15-01-2010')
INSERT @B VALUES (2 ,'15-10-2010','20-10-2010')

;WITH transitions as
(
    SELECT *, ROW_NUMBER() OVER (PARTITION BY AID ORDER BY startdate) Sequence
    FROM (
        SELECT A.AID, A.startdate
        FROM @a A
        UNION
        SELECT A.AID, B.startdate + 1
        FROM @A A
        INNER JOIN @b B ON B.startdate > A.startdate AND B.startdate < A.enddate
        UNION
        SELECT A.AID, B.enddate + 1
        FROM @A A
        INNER JOIN @b B ON B.enddate > A.startdate AND B.enddate < A.enddate
        UNION
        SELECT A.AID, A.enddate + 1
        FROM @a A
        WHERE A.enddate > A.startdate
    ) T
)   
SELECT T1.AID, T1.startdate startdate, T2.startdate - 1 enddate
FROM transitions T1
INNER JOIN transitions T2 ON T2.AID = T1.AID AND T2.Sequence = T1.Sequence + 1
ORDER BY T1.AID, T1.Sequence
+2
source

Source: https://habr.com/ru/post/1776214/


All Articles