在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
在上面的记录中,如果它们具有相同的UUID和update_time(因为有重复的数据插入),我想删除这些记录。
例如UUID = 61075026156000具有相同的update_time7次;我想删除6条记录,仅留下一条。这样,如果所有UUID都相同,我想删除它们update_time-仅保留一个。
您可以这样操作ROW_NUMBER():
with cte as (
select *, row_number() over (partition by uuid, updated_time order by id) rn
from tablename
)
delete from cte
where rn > 1
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。