2017-07-19 5 views
1

Ich habe eine Tabelle mit IDs und mit null Startdatum. Ich versuche, sie mit CTE zu füllen.Backfill-Daten mit CTE in SQL Server

Wenn die EndDate sind '2011-12-31' für Datensatz # 1 ist das Startdatum für Datensatz # 2 EndDate von Datensatz # 1 + 1 Tag dh '2012-01-01'

Create table dbo.input(inputid int null,startDate smalldatetime null,endDate smalldatetime null) 
insert into dbo.input values(111,null,'2011-05-31') 
insert into dbo.input values(111,null,'2012-05-31') 
insert into dbo.input values(111,null,'2013-05-31') 
insert into dbo.input values(111,null,'2014-05-31') 
insert into dbo.input values(111,null,'2015-05-31') 


insert into dbo.input values(222,null,'2010-06-30') 
insert into dbo.input values(222,null,'2011-06-30') 
insert into dbo.input values(222,null,'2012-06-30') 
insert into dbo.input values(222,null,'2013-06-30') 
insert into dbo.input values(222,null,'2014-06-30') 

Dies ist die erwartete Ausgabe.

Create table dbo.output(outputid int null,startDate smalldatetime null,endDate smalldatetime null) 
insert into dbo.output(111,null,'2011-05-31') 
insert into dbo.output(111,'2011-06-01','2012-05-31') 
insert into dbo.output(111,'2012-05-31','2013-05-31') 
insert into dbo.output(111,'2013-05-31','2014-05-31') 
insert into dbo.output(111,'2014-05-31','2015-05-31') 


insert into dbo.output(222,null,'2010-06-30') 
insert into dbo.output(222,'2010-06-30','2011-06-30') 
insert into dbo.output(222,'2011-06-30','2012-06-30') 
insert into dbo.output(222,'2012-06-30','2013-06-30') 
insert into dbo.output(222,'2013-06-30','2014-06-30') 

Das ist, was ich versucht

WITH CTE AS (
SELECT 
     rn = ROW_NUMBER() OVER (partition by p.inputid ORDER BY p.inputid,endDate), 
     p.inputid 
     ,EndDate 
FROM dbo.input p 
) 
SELECT distinct 
DATEADD(day,1,prev.enddate) as startd,cte.inputid,cte.endDate 
FROM CTE 
LEFT JOIN CTE prev ON prev.rn = CTE.rn - 1 
LEFT JOIN CTE nex ON nex.rn = CTE.rn + 1 
order by cte.inputid,startd 

Es ist nicht richtig durch inputid Gruppierung und ich weiß nicht, wie dieses Problem beheben?

Jede Hilfe wird geschätzt.

Dank MR

Antwort

1

Anpassen des vorhandenen Abfrage, aber die Ergebnisse sind nicht genau die gleichen wie du es erwartet hast. Sollte StartDate am nächsten Tag des vorherigen EndDates sein? (nicht der gleiche Tag wie das vorherige EndDate.)

;WITH cte AS (SELECT 
        rn = ROW_NUMBER() OVER (partition by inputid ORDER BY endDate) 
       , * 
      FROM #input 
) 

SELECT a.inputid, DATEADD(DD, 1, b.endDate) as startDate , a.endDate 
FROM CTE a 
LEFT JOIN CTE b 
    ON a.inputid = b.inputid and a.rn = b.rn + 1 
ORDER BY a.inputid, a.startDate; 
0

Solange Sie SQL Server 2012 oder höher verwenden, können Sie die folgenden Befehle verwenden ...

SELECT 
    i.inputid, 
    startDate = DATEADD(dd, 
         CASE WHEN ROW_NUMBER() OVER (ORDER BY i.inputid, i.endDate) = 2 THEN 1 ELSE 0 END, 
         LAG(i.endDate, 1) OVER (PARTITION BY i.inputid ORDER BY i.endDate) 
         ), 
    i.endDate 
FROM 
    #input i;