合并数据库中的重复时间记录

时间:2014-10-09 08:02:51

标签: sql sql-server sql-server-2008

我有一个临时数据库表,其中一些数据是重复的。

EmployeeId   StartDate   EndDate     Column1   Column2
1000         2009/05/01  2010/04/30   X         Y
1000         2010/05/01  2011/04/30   X         Y
1000         2011/05/01  2012/04/30   X         X
1000         2012/05/01  2013/04/30   X         Y
1000         2013/05/01  NULL         X         X

如上所示,有些行是冗余的,可以合并形成一行而不违反数据有效性。我希望尽可能合并这些行,结果应该如下所示

EmployeeId   StartDate   EndDate     Column1   Column2
1000         2009/05/01  2011/04/30   X         Y
1000         2011/05/01  2012/04/30   X         X
1000         2012/05/01  2013/04/30   X         Y
1000         2013/05/01  NULL         X         X

如何实现这一目标?

2 个答案:

答案 0 :(得分:1)

如果您可以确保所有开始日期和结束日期都是连续的,请尝试此操作:

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

答案 1 :(得分:0)

试试这个

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)

注意:TEMP是包含上述列的表