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

一. 删除完全重复的记录

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

复制代码 代码如下:

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

时间: 2024-11-01 08:19:16

sql删除重复数据的详细方法_MsSql的相关文章

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 dup

SQL删除重复数据方法

原文:SQL删除重复数据方法 例如: id           name         value 1               a                 pp 2               a                 pp 3               b                 iii 4               b                 pp 5               b                 pp 6            

sql 删除重复数据

sql 删除重复数据 本文章提供一款mysql删除重复数据,就是数据表中存在二条以上的重复数据,删除编号最小的一条信息. delete `borough_name` as a from fke_borough as a, ( select *,min(id) from fke_borough group by `borough_name` having count(1) > 1 ) as b  where a.`borough_name` = b.`borough_name` and a.id

MySQL中删除重复数据的简单方法_Mysql

MYSQL里有五百万数据,但大多是重复的,真实的就180万,于是想怎样把这些重复的数据搞出来,在网上找了一圈,好多是用NOT IN这样的代码,这样效率很低,自己琢磨组合了一下,找到一个高效的处理方式,用这个方式,五百万数据,十来分钟就全部去除重复了,请各位参考. 第一步:从500万数据表data_content_152里提取出不重复的字段SFZHM对应的ID字段到TMP3表 create table tmp3 as select min(id) as col1 from data_content

sql 删除重复数据与查找重复数据的方法(1/4)

例1,表中有主键(可唯一标识的字段),且该字段为数字类型 例1测试数据 /* 表结构 */ drop table if exists `t1`; create table if not exists `t1`(   `id` int(1) not null auto_increment,   `name` varchar(20) not null,   `add` varchar(20) not null,   primary key(`id`) )engine=innodb; /* 插入测试数

sql过滤重复数据几种方法

在开发中经常会碰到关于有重复记录的数据,我们重想办法来过滤一些重复的记录,下面我总结了几种方法,希望对大家有用. 方法一,   select identity(int,1,1) as id,* into #temp from tabel select * from #temp where id in ( select max(id) from #emp where having count(*)>1 group by col1,col2,col3...) 实例 有一个表的结构是这样的      

SQL删除重复数据只保留一条

  用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) 2.删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只

MYSQL删除重复数据的简单方法_Mysql

复制代码 代码如下: CREATETABLE`users`(`id`int(10)NOTNULLAUTO_INCREMENT,`name`char(50)NOTNULL,PRIMARYKEY(`id`)) 复制代码 代码如下: deletefromuserswhereidin(selectmin(id)fromusersgroupbynamehavingcount(name)>1); 结果报错:1093youcan'tspecifytargettable.... 原因是mysql删除动作不能带有

删除重复数据sql语句

删除重复数据sql语句 方法一 假设有重复的字段为name,address,要求得到这两个字段唯一的结果集 select identity(int,1,1) as autoid, * into #tmp from tablename select min(autoid) as autoid into #tmp2 from #tmp group by name,autoid select * from #tmp where autoid in(select autoid from #tmp2) 方