这篇文章主要为大家详细介绍了SQL Server SQL去除重复记录的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!
话不多说,请看代码:
代码如下:
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),保留最小一条
delete a from #T a where exists(select 1 from #T where Name=a.Name and ID<a.ID)
代码如下:
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
delete a from #T a where ID not in (select min(ID) from #T where Name=a.Name)
delete a from #T a where ID not in(select min(ID)from #T group by Name)
delete a from #T a where (select count(1) from #T where Name=a.Name and ID<a.ID)>0
delete a from #T a where ID<>(select top 1 ID from #T where Name=a.name order by ID)
代码如下:
delete a from #T a where ID>any(select ID from #T where Name=a.Name)
select * from #T
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持四海网!
本文来自:http://www.q1010.com/179/9264-0.html
注:关于SQL Server SQL去除重复记录的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:SQL SERVER
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。