sql删除重复数据的详细方法

一. 删除完全重复的记录

完全重复的数据,通常是由于没有设置主键/唯一键约束导致的。
测试数据:

复制代码 代码如下:

if object_id(‘duplicate_all’) is not null

drop table duplicate_all

go

create table duplicate_all

(

c1 int,

c2 int,

c3 varchar(100)

)

go

insert into duplicate_all

select 1,100,’aaa’ union all

select 1,100,’aaa’ union all

select 1,100,’aaa’ union all

select 1,100,’aaa’ union all

select 1,100,’aaa’ union all

select 2,200,’bbb’ union all

select 3,300,’ccc’ union all

select 4,400,’ddd’ union all

select 5,500,’eee’

go

(1) 借助临时表

利用distinct得到单条记录,删除源数据,然后导回不重复记录。
如果表不大的话,可以把所有记录导出一次,然后truncate表后再导回,这样可以避免delete的日志操作。

复制代码 代码如下:

if object_id(‘tempdb..#tmp’) is not null

drop table #tmp

go

select distinct * into #tmp

from duplicate_all

where c1 = 1

go

delete duplicate_all where c1 = 1

go

insert into duplicate_all

select * from #tmp

(2) 使用row_number


复制代码 代码如下:

with tmp

as

(

select *,row_number() over(partition by c1,c2,c3 order by(getdate())) as num

from duplicate_all

where c1 = 1

)

delete tmp where num > 1

如果多个表有完全重复的行,可以考虑通过union将多个表联合,插到一个新的同结构的表,sql server会帮助去掉表和表之间的重复行。

二. 删除部分重复的记录

部分列重复的数据,通常表上是有主键的,可能是程序逻辑造成了多行数据列值的重复。
测试数据:

复制代码 代码如下:

if object_id(‘duplicate_col’) is not null

drop table duplicate_col

go

create table duplicate_col

(

c1 int primary key,

c2 int,

c3 varchar(100)

)

go

insert into duplicate_col

select 1,100,’aaa’ union all

select 2,100,’aaa’ union all

select 3,100,’aaa’ union all

select 4,100,’aaa’ union all

select 5,500,’eee’

go

(1) 唯一索引

唯一索引有个忽略重复建的选项,在创建主键约束/唯一键约束时都可以使用这个索引选项。

复制代码 代码如下:

if object_id(‘tmp’) is not null

drop table tmp

go

create table tmp

(

c1 int,

c2 int,

c3 varchar(100),

constraint uq_01 unique(c2,c3) with(ignore_dup_key = on)

)

go

insert into tmp

select * from duplicate_col

select * from tmp

(2) 借助主键/唯一键来删除

通常会选择主键/唯一键的最大/最小值保留,其他行删除。以下只保留重复记录中c1最小的行。


复制代码 代码如下:

delete from duplicate_col

where exists(select 1 from duplicate_col b where duplicate_col.c1 > b.c1 and (duplicate_col.c2 = b.c2 and duplicate_col.c3 = b.c3))

–或者


复制代码 代码如下:

delete from duplicate_col

where c1 not in (select min(c1) from duplicate_col group by c2,c3)

如果要保留重复记录中的第n行,可以参考05.取分组中的某几行。

(3) row_number

和删除完全重复记录的写法基本一样。


复制代码 代码如下:

with tmp

as

(

select *,row_number() over(partition by c2,c3 order by(getdate())) as num

from duplicate_col

)

delete tmp where num > 1

select * from duplicate_col


sql删除重复数据只保留一条 (下面的代码,很多网友反馈错误,大家多测试)


用sql语句,删除掉重复项只保留一条

在几千条记录里,存在着些相同的记录,如何能用sql语句,删除掉重复的呢

1、查找表中多余的重复记录,重复记录是根据单个字段(peopleid)来判断

select * from people

where peopleid in (select peopleid from people group by peopleid having count(peopleid) > 1)

2、删除表中多余的重复记录,重复记录是根据单个字段(peopleid)来判断,只留有rowid最小的记录

delete from people

where   peoplename in (select peoplename    from people group by peoplename      having count(peoplename) > 1)

and   peopleid not in (select min(peopleid) from people group by peoplename     having count(peoplename)>1)

3、查找表中多余的重复记录(多个字段)

select * from vitae a

where (a.peopleid,a.seq) in (select peopleid,seq from vitae group by peopleid,seq having count(*) > 1)

4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录

delete from vitae a

where (a.peopleid,a.seq) in (select peopleid,seq from vitae group by peopleid,seq having count(*) > 1)

and rowid not in (select min(rowid) from vitae group by peopleid,seq having count(*)>1)

5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录

select * from vitae a

where (a.peopleid,a.seq) in (select peopleid,seq from vitae group by peopleid,seq having count(*) > 1)

and rowid not in (select min(rowid) from vitae group by peopleid,seq having count(*)>1)  

6.消除一个字段的左边的第一位:

update tablename set [title]=right([title],(len([title])-1)) where title like ‘村%’

7.消除一个字段的右边的第一位:

update tablename set [title]=left([title],(len([title])-1)) where title like ‘%村’

8.假删除表中多余的重复记录(多个字段),不包含rowid最小的记录

update vitae set ispass=-1

where peopleid in (select peopleid from vitae group by peopleid

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐