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

WBOY
풀어 주다: 2016-06-07 16:22:00
원래의
1601명이 탐색했습니다.

重复数据,通常有两种:一是完全重复的记录,也就是所有字段的值都一样;二是部分字段值重复的记录。 一. 删除完全重复的记录 完全重复的数据,通常是由于没有设置主键/唯一键约束导致的。 测试数据: if OBJECT_ID('duplicate_all') is not null drop table

   重复数据,通常有两种:一是完全重复的记录,也就是所有字段的值都一样;二是部分字段值重复的记录。

  一. 删除完全重复的记录

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

  测试数据:

  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

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!