sql删除重复数据的详细方法
发布时间:2024-01-14 15:22:21
一. 删除完全重复的记录
完全重复的数据,通常是由于没有设置主键/唯一键约束导致的。
测试数据:
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


猜你喜欢
- 元组(Tuple)元组是有序且不可更改的集合。在 Python 中,元组是用圆括号编写的。实例创建元组:thistuple = ("
- 说起元宵节,各位有没有觉得这是咱们中国人最浪漫的节日呢?国人向来拘谨古板,一年到头都是小心谨慎地过日子,唯有元宵节这天可以纵情豪放一把。东风
- 第一步:建与mysql同构的oracle数据库实例,并更新数据。 1.期望将my
- 本文实例为大家分享了python使用Plotly绘图工具绘制柱状图的具体代码,供大家参考,具体内容如下使用Plotly绘制基本的柱状图,需要
- 要自动签到,最简单的是打开页面分析请求,然后我们用脚本实现请求的自动化。但是发现食行没有页面,只有 APP,这不是一个好消息,这意味着需要抓
- python3 默认的是UTF-8格式,但在在用dump写入的时候仍然要注意:如下import jsondata1 = { "Te
- JAN-1(January) FEB-2(February) MAR-3(March)APR-4(April) MAY-5(Ma
- 视觉设计是什么,人们怎么认为它的,自己又是怎么对待和理解它,它的核心价值是什么。视觉设计,冒似很艺术,跟艺术相关的职业,给大多数人的印象是做
- 实例如下所示:# -*- coding: utf-8 -*-#to find where use the table on xxxxx xx
- 分别为:1.倒计定时器:timename=setTimeout("function();",delaytime);2.循
- 这篇文章主要介绍了通过Kettle自定义jar包供javascript使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参
- 一、Django密码存储和加密方式#算法+迭代+盐+加密<algorithm>$<iterations>$<s
- Pyinstallerpyinstaller是python的一个第三方模块,使用它可以将python程序打包为可执行文件,实现打包后的程序在
- SMTPSMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。Python对SMTP
- 写在前面在写go的时候经常用到序列化、反序列化,记录一下遇到过的坑。空指针会被解析成字符串"null"type Pers
- 一般情况下,当数据表中,莫一列被设置成了标识列之后,是无法向标识列中手动的去插入标识列的显示值。但是,可以通过设置SET IDENTITY_
- 前言学学Python中操纵JSON的知识。学完本文,你可以学到如下内容:1、JSON是什么?2、JSON与XML的优劣差异?3、将Pytho
- 设计是一个输入-输出的过程,因为首先有用户的需求,客户的项目才有设计的产生,设计是带有目的性和市场行为的,当然也有一部分的创造性设计,仅仅为
- 基本用法?在大型项目中,我们可能需要拆分应用为更小的块,并仅在需要时再从服务器加载相关组件。Vue 提供了defineAsyncCompon
- 一、利用外键约束更新MySQL中的数据现在,最流行的开源关系型数据库管理系统非MySQL莫属,而MySQL又支持多个存储引擎,其中默认的也是