sql >> Database teknologi >  >> RDS >> Sqlserver

Flet dublerede tidsmæssige poster i databasen

Prøv dette, hvis du kan sikre, at alle startdato og slutdato er fortløbende :

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. SqlDependency udløser ikke OnChange-hændelsen, når datasættet ændres

  2. mysqli forberedt sætning num_rows returnerer 0, mens forespørgslen returnerer større end 0

  3. automatisk udfylde tabel med dummy data i mysql

  4. Sequelize grænse og udligne forkert placering i forespørgslen