Merge duplicate temporal records in database

后端 未结 2 1766
走了就别回头了
走了就别回头了 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:43

    Try this if you can ensure that all the start date and end date are continuous :

    with  t1 as  --tag first row with 1 in a continuous time series
    (
    select t1.*, case when t1.column1=t2.column1 and t1.column2=t2.column2
                      then 0 else 1 end as tag
      from your_table t1
      left join your_table t2
        on t1.EmployeeId= t2.EmployeeId and dateadd(day,-1,t1.StartDate)= t2.EndDate
    )
    select t1.EmployeeId, t1.StartDate, 
           case when min(T2.StartDate) is null then null
                else dateadd(day,-1,min(T2.StartDate)) end as EndDate,
           t1.Column1, t1.Column2
      from (select t1.* from t1 where tag=1 ) as t1  -- to get StartDate
      left join (select t1.* from t1 where tag=1 ) as t2  -- to get a new EndDate
        on t1.EmployeeId= t2.EmployeeId and t1.StartDate < t2.StartDate
     group by t1.EmployeeId, t1.StartDate, t1.Column1,   t1.Column2
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题