如何删除完全重复的数据
作为DBA,想必有时候面试会被问到如何删除一张表中完全重复的数据?你能想到几种方法呢?
一条命令删除完全重复的数据
准备测试数据:
use tempdb
go
if object_id('test1') is not null drop table test1
go
create table test1(a int, b int, c int, d int)
go
insert into test1 values
(1, 2, 3, 4),
(1, 2, 3, 4),
(5, 6, 7, 8),
(5, 6, 7, 8)
go
一条命令删除完全重复的数据:
with cte as ( select *, row_number() over(partition by a,b,c order by a) rn from test1 )
delete cte where rn > 1
执行 select * from test1
查看结果如下:
a | b | c | d |
---|---|---|---|
1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 |
删除字段b和c完全重复(保留字段a最大)的数据
准备测试数据:
use tempdb
go
if object_id('test2') is not null drop table test2
go
create table test2(a int, b int, c int, d int)
go
insert into test2 values
(1, 2, 3, 4),
(2, 2, 3, 5),
(3, 6, 7, 9),
(4, 6, 7, 8)
go
一条命令删除b和c完全重复、同时保留字段a最大的数据:
with cte as ( select *, row_number() over(partition by b,c order by a desc) rn from test2 )
delete cte where rn > 1
执行 select * from test2
查看结果如下:
a | b | c | d |
---|---|---|---|
2 | 2 | 3 | 5 |
4 | 6 | 7 | 8 |