≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 数据库 > SQL Server

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

人气:315 时间:2020-03-11

这篇文章主要为大家详细介绍了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

 

本文来自:http://www.q1010.com/179/8447-0.html

注:关于sql删除重复数据的详细方法的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:SQL SERVER

您可能感兴趣的文章

  • 浅析SQLServer中的Scanf与Printf
  • 解析SQLServer获取Excel中所有Sheet的方法
  • SQLSERVER数据库备份后无法还原的解决办法
  • 浅析被遗忘的SQLServer比较运算符修饰词
  • sqlserver 存储过程中If Else的用法实例
  • SQL Server 将ACCESS数据库迁移到SQLSERVER数据库两种方法
  • SQL Server 使用mongovue把sqlserver数据导入mongodb的简单示例
  • SQL Server driver配置方法 jdbc连接sqlserver的简单示例
  • SQL Server 查找查询死锁源头的方法 sqlserver死锁监控
  • Linux环境中使用BIEE 连接SQLServer业务数据源的简单示例
上一篇:获取SQL Server表字段的各种属性实例代码
下一篇:基于SQL Server中char,nchar,varchar,nvarchar的使用区别
热门文章
  • SQL Server SQL获取第一条记录的方法
  • SQL Server出现System.OutOfMemoryException异常的解决方法
  • SQL Server的 update from 语句的简单示例
  • SQL Server 数据库备份方法菜鸟教程
  • SQL Server 多表关联时在where语句中慎用trim()方法
  • SQL Server数据类型及长度限制详细说明
  • mybaits非配置原因,导致SqlSession was not registered for synchronization异常解析
  • SQL Server 收缩后对数据库的使用有影响吗?
  • SQL Server 格式导致的Excel导入sql出现异常的解决方法
  • SQL Server 连接服务器出现错误 7391的解决方法
  • 最新文章
    • SQL Server存储过程基本语法的简单示例
    • sql查询时增加自动编号和分页的简单示例
    • sql轻松应付百万数据的高效数据分页存储过程的简单示例
    • sql获取一条数据中所有字段的名称和值的实现方法
    • sql分割函数的简单示例
    • SQL Server异常捕获的简单示例
    • SQL SERVER回滚恢复误操作数据的实现方法
    • SQL Server函数或存储过程中抛出异常的实现方法
    • SQL Server创建数据库的完整代码
    • SQL Server创建数据库的命令用法示例

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。