在SQL Server中,删除日期的重复项,每个日期只留下一条记录

在SQL Server中,删除日期的重复项,每个日期只留下一条记录,sql,sql-server,Sql,Sql Server,在SQL Server中,我有如下数据: ID uid UUID downtime updated_time received time status ---------------------------------------------------------------------------------------------------- 4794 6501 61075024966

在SQL Server中,我有如下数据:

ID      uid     UUID           downtime   updated_time               received time            status
----------------------------------------------------------------------------------------------------
4794    6501    61075024966012    60      2019-12-27 06:00:02.813    2019-12-23 05:07:46.120    0
4332    0354    61075026156000    1440    2019-12-27 06:00:02.813    2019-12-23 17:49:56.393    0
4333    0354    61075026156000    1440    2019-12-27 06:00:02.813    2019-12-23 17:49:56.393    0
4334    0354    61075026156000    1440    2019-12-27 06:00:02.813    2019-12-23 17:49:56.393    0
4335    0354    61075026156000    1440    2019-12-27 06:00:02.813    2019-12-23 17:49:56.393    0
4336    0354    61075026156000    1440    2019-12-27 06:00:02.813    2019-12-23 17:49:56.393    0
4337    0354    61075026156000    1440    2019-12-27 06:00:02.813    2019-12-23 17:49:56.393    0
4338    0354    61075026156000    1440    2019-12-27 06:00:02.813    2019-12-23 17:49:56.393    0
4345    0355    61075026156001    1440    2019-12-27 07:00:02.813    2019-12-23 19:49:56.393    0
4346    0355    61075026156001    1440    2019-12-27 07:00:02.813    2019-12-23 19:49:56.393    0
4347    0355    61075026156001    1440    2019-12-27 07:00:02.813    2019-12-23 19:49:56.393    0
4348    0355    61075026156001    1440    2019-12-27 07:00:02.813    2019-12-23 19:49:56.393    0
4355    0358    61075026156221    23      2019-12-27 07:00:02.813    2019-12-23 19:49:56.393    0
4365    0358    61075026156221    35      2019-12-29 07:00:02.813    2019-12-23 19:49:56.393    0
with cte as (
  select *, row_number() over (partition by uuid, updated_time order by id) rn
  from tablename  
)
delete from cte
where rn > 1
在上面的记录中,我想删除那些UUID和update_时间与重复数据插入相同的记录


例如,UUID=61075026156000有7次相同的更新时间;我想删除6条记录,只留下一条。像这样,如果所有UUID都有相同的更新时间,我想删除它们-只留下一个。

您可以使用聚合:

select min(id), uid, uuid, downtime, updated_time
from t
group by uid, uuid, downtime, updated_time;
这将获得四列中的重复项-这似乎是您想要的。您也可以仅限于您提到的两列:

select min(id), min(uid), uuid, min(downtime), updated_time
from t
group by uid, uuid, downtime, updated_time;
或使用窗口功能:

select t.*
from (select t.*
             row_number() over (partition by uuid, updatedtime order by (select null)) as seqnum
      from t
     ) t
where seqnum = 1;
您可以使用如下所示的行号来执行此操作:

ID      uid     UUID           downtime   updated_time               received time            status
----------------------------------------------------------------------------------------------------
4794    6501    61075024966012    60      2019-12-27 06:00:02.813    2019-12-23 05:07:46.120    0
4332    0354    61075026156000    1440    2019-12-27 06:00:02.813    2019-12-23 17:49:56.393    0
4333    0354    61075026156000    1440    2019-12-27 06:00:02.813    2019-12-23 17:49:56.393    0
4334    0354    61075026156000    1440    2019-12-27 06:00:02.813    2019-12-23 17:49:56.393    0
4335    0354    61075026156000    1440    2019-12-27 06:00:02.813    2019-12-23 17:49:56.393    0
4336    0354    61075026156000    1440    2019-12-27 06:00:02.813    2019-12-23 17:49:56.393    0
4337    0354    61075026156000    1440    2019-12-27 06:00:02.813    2019-12-23 17:49:56.393    0
4338    0354    61075026156000    1440    2019-12-27 06:00:02.813    2019-12-23 17:49:56.393    0
4345    0355    61075026156001    1440    2019-12-27 07:00:02.813    2019-12-23 19:49:56.393    0
4346    0355    61075026156001    1440    2019-12-27 07:00:02.813    2019-12-23 19:49:56.393    0
4347    0355    61075026156001    1440    2019-12-27 07:00:02.813    2019-12-23 19:49:56.393    0
4348    0355    61075026156001    1440    2019-12-27 07:00:02.813    2019-12-23 19:49:56.393    0
4355    0358    61075026156221    23      2019-12-27 07:00:02.813    2019-12-23 19:49:56.393    0
4365    0358    61075026156221    35      2019-12-29 07:00:02.813    2019-12-23 19:49:56.393    0
with cte as (
  select *, row_number() over (partition by uuid, updated_time order by id) rn
  from tablename  
)
delete from cte
where rn > 1
看。 结果:

使用存在

  delete t
  from tablename t
  where exists (select 1 
                from tablename tt 
                where tt.uuid = t.uuid and tt.updatedtime = t.updatedtime and tt.id < t.id)