Merge duplicate temporal records in database

后端 未结 2 1772
走了就别回头了
走了就别回头了 2020-12-20 06:18

I have a temporal database table where some of the data is duplicated.

EmployeeId   StartDate   EndDate     Column1   Column2
1000         2009/05/01  2010/0         


        
2条回答
  •  抹茶落季
    2020-12-20 06:48

    Try this

    SELECT A.EmployeeId,A.StartDate,A.EndDate,A.Column1,A.Column2 FROM (SELECT EmployeeId,StartDate,EndDate,Column1,Column2 FROM TEMP GROUP BY EmployeeId,StartDate,EndDate,Column1,Column2)A
    JOIN 
    (SELECT Y.EmployeeId,Y.StartDate,Y.EndDate,Y.Column1,Y.Column2 FROM TEMP X JOIN TEMP Y ON X.EmployeeId=Y.EmployeeId AND DATEADD(day,1,X.EndDate)=Y.StartDate)B 
    ON A.EndDate=DATEADD(day,-1,B.StartDate)
    

    Note: TEMP is the table with mentioned columns

提交回复
热议问题