sqlserver 删除重复记录处理(转)

注:此处“重复”非完全重复,意为某字段数据重复
  HZT表结构
ID int
Title nvarchar(50)
AddDate datetime
  数据
一. 查找重复记录
  1. 查找全部重复记录
Select * From 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1)

2. 过滤重复记录(只显示一条)
Select * From HZT Where ID In (Select Max(ID) From HZT Group By Title)
注:此处显示ID最大一条记录
二. 删除重复记录
  1. 删除全部重复记录(慎用)
Delete 表 Where 重复字段 In (Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1)

  2. 保留一条(这个应该是大多数人所需要的)
Delete HZT Where ID Not In (Select Max(ID) From HZT Group By Title)
  注:此处保留ID最大一条记录

其它相关:

删除重复记录有大小关系时,保留大或小其中一个记录

--> --> (Roy)生成測試數據

if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))
Insert #T
select 1,N'A',N'A1' union all
select 2,N'A',N'A2' union all
select 3,N'A',N'A3' union all
select 4,N'B',N'B1' union all
select 5,N'B',N'B2'
Go

--I、Name相同ID最小的记录(推荐用1,2,3),保留最小一条
方法1:
delete a from #T a where exists(select 1 from #T where Name=a.Name and ID<a.ID)
方法2:
delete a from #T a left join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID
where b.Id is null
方法3:
delete a from #T a where ID not in (select min(ID) from #T where Name=a.Name)
方法4(注:ID为唯一时可用):
delete a from #T a where ID not in(select min(ID)from #T group by Name)
方法5:
delete a from #T a where (select count(1) from #T where Name=a.Name and ID<a.ID)>0
方法6:
delete a from #T a where ID<>(select top 1 ID from #T where Name=a.name order by ID)
方法7:
delete a from #T a where ID>any(select ID from #T where Name=a.Name)
select * from #T

生成结果:
/*
ID Name Memo
----------- ---- ----
1 A A1
4 B B1

(2 行受影响)
*/

--II、Name相同ID保留最大的一条记录:
方法1:
delete a from #T a where exists(select 1 from #T where Name=a.Name and ID>a.ID)
方法2:
delete a from #T a left join (select max(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID
where b.Id is null
方法3:
delete a from #T a where ID not in (select max(ID) from #T where Name=a.Name)
方法4(注:ID为唯一时可用):
delete a from #T a where ID not in(select max(ID)from #T group by Name)
方法5:
delete a from #T a where (select count(1) from #T where Name=a.Name and ID>a.ID)>0
方法6:
delete a from #T a where ID<>(select top 1 ID from #T where Name=a.name order by ID desc)
方法7:
delete a from #T a where ID<any(select ID from #T where Name=a.Name)
select * from #T
/*
ID Name Memo
----------- ---- ----
3 A A3
5 B B2

(2 行受影响)
*/

--3、删除重复记录没有大小关系时,处理重复值
--> --> (Roy)生成測試數據

if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([Num] int,[Name] nvarchar(1))
Insert #T
select 1,N'A' union all
select 1,N'A' union all
select 1,N'A' union all
select 2,N'B' union all
select 2,N'B'
Go

方法1:
if object_id('Tempdb..#') is not null
drop table #
Select distinct * into # from #T--排除重复记录结果集生成临时表#
truncate table #T--清空表
insert #T select * from # --把临时表#插入到表#T中

--查看结果
select * from #T

/*
Num Name
----------- ----
1 A
2 B

(2 行受影响)
*/

--重新执行测试数据后用方法2
方法2:
alter table #T add ID int identity--新增标识列
go
delete a from #T a where exists(select 1 from #T where Num=a.Num and Name=a.Name and ID>a.ID)--只保留一条记录
go
alter table #T drop column ID--删除标识列

--查看结果
select * from #T

/*
Num Name
----------- ----
1 A
2 B

(2 行受影响)

*/

--重新执行测试数据后用方法3
方法3:
declare Roy_Cursor cursor local for
select count(1)-1,Num,Name from #T group by Num,Name having count(1)>1
declare @con int,@Num int,@Name nvarchar(1)
open Roy_Cursor
fetch next from Roy_Cursor into @con,@Num,@Name
while @@Fetch_status=0
begin
set rowcount @con;
delete #T where Num=@Num and Name=@Name
set rowcount 0;
fetch next from Roy_Cursor into @con,@Num,@Name
end
close Roy_Cursor
deallocate Roy_Cursor

--查看结果
select * from #T
/*
Num Name
----------- ----
1 A
2 B

(2 行受影响)
*/

(0)

相关推荐

  • mysql删除重复记录语句的方法

    例如: id name value 1 a pp 2 a pp 3 b iii 4 b pp 5 b pp 6 c pp 7 c pp 8 c iii id是主键 要求得到这样的结果 id name value 1 a pp 3 b iii 4 b pp 6 c pp 8 c iii 方法1 delete YourTable where [id] not in ( select max([id]) from YourTable group by (name + value)) 方法2 delet

  • SQL语句实现删除重复记录并只保留一条

    复制代码 代码如下: delete WeiBoTopics where Id in(select max(Id) from WeiBoTopics group by WeiBoId,Title having COUNT(*) > 1); SQL:删除重复数据,只保留一条用SQL语句,删除掉重复项只保留一条在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 复制代码 代码如下: select * fr

  • SqlServer2005中使用row_number()在一个查询中删除重复记录的方法

    下面我们来看下,如何利用它来删除一个表中重复记录: 复制代码 代码如下: If Exists(Select * From tempdb.Information_Schema.Tables Where Table_Name Like '#Temp%') Drop Table #temp Create Table #temp ([Id] int, [Name] varchar(50), [Age] int, [Sex] bit default 1) Go Insert Into #temp ([Id

  • SQL Server2008中删除重复记录的方法分享

    现在让我们来看在SQL SERVER 2008中如何删除这些记录, 首先,可以模拟造一些简单重复记录: 复制代码 代码如下: Create Table dbo.Employee ( [Id] int Primary KEY , [Name] varchar(50), [Age] int, [Sex] bit default 1 ) Insert Into Employee ([Id] , [Name] , [Age] , [Sex] ) Values(1,'James',25,default)

  • MySQL数据库中删除重复记录的方法总结[推荐]

    表结构: mysql> desc demo; +-------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+----------------+ | id | int(11) unsigned | NO | PRI | NULL

  • SqlServer 2005中使用row_number()在一个查询中删除重复记录

    下面我们来看下,如何利用它来删除一个表中重复记录: 复制代码 代码如下: If Exists(Select * From tempdb.Information_Schema.Tables Where Table_Name Like '#Temp%') Drop Table #temp Create Table #temp ([Id] int, [Name] varchar(50), [Age] int, [Sex] bit default 1) Go Insert Into #temp ([Id

  • 有用的SQL语句(删除重复记录,收缩日志)

    删除重复记录,将TABLE_NAME中的不重复记录保存到#TABLE_NAME中 select distinct * into #table_name from table_name delete from table_name select * into table_name from #table_name drop table #table_name 与此相关的是"select into"选项,可以在数据库属性 对话框中,勾起来此项,或者在Query Analyzer中执行 ex

  • SQL语句实现删除ACCESS重复记录的两种方法

    以下就重复记录删除的问题作一阐述. 有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略. 1.对于第一种重复,比较容易解决,使用 select distinct * from tableName 就可以得到无重复记录的结果集. 如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除 复制代码 代码如下: select distinct * into #Tmp from table

  • sql 删除表中的重复记录

    遇见了表中存在重复的记录的问题,直接写sql删除时最快的,才不要慢慢的复制到excel表中慢慢的人工找呢 如下sql,找出重复的记录,和重复记录中ID值最小的记录(表中ID为自增长) select MIN(ID) as id, StructSN ,Date,UserID,StarCount,COUNT(StructSN) as c from T_Dor_StructStar where Date >= '20160919' group by StructSN ,Date,UserID,StarC

  • SQL对冗余数据的删除重复记录只保留单条的说明

    我们先看一下相关数据结构的知识. 在学习线性表的时候,曾有这样一个例题. 已知一个存储整数的顺序表La,试构造顺序表Lb,要求顺序表Lb中只包含顺序表La中所有值不相同的数据元素. 算法思路: 先把顺序表La的第一个元素付给顺序表Lb,然后从顺序表La的第2个元素起,每一个元素与顺序表Lb中的每一个元素进行比较,如果不相同,则把该元素附加到顺序表Lb的末尾. 复制代码 代码如下: public SeqList<int> Purge(SeqList<int> La) { SeqLis

随机推荐