日期:2014-05-18  浏览次数:20549 次

写一条sql语句去除一张表中的重复数据
如题。。。。。

但是要注意的是,不是仅仅显示不重复的数据,是发现有重复的数据删除一条重复的。

比如:

Username Department

Roy COM
Alwin UDDI
Roy COM

写一条sql语句删除roy --- com.

thanks a lot.

------解决方案--------------------
SQL code
select distinct * into # from tb
go
truncate table tb
go
insert tb select * from #
go
drop table #

------解决方案--------------------
探讨
SQL codeselectdistinct*into #from tbgotruncatetable tbgoinsert tbselect*from #godroptable #

------解决方案--------------------
探讨
SQL codeselectdistinct*into #from tbgotruncatetable tbgoinsert tbselect*from #godroptable #

------解决方案--------------------
出现完全相同的?

select distinct * into tmp from tb
truncate table tb
insert into tb select * from tmp
------解决方案--------------------
SQL code

create table test(username varchar(20),department varchar(20))
insert test
select 'roy','com' union all
select 'Alwin','UUDI' union all
select 'roy','com' 
go

delete test where username in (select username from test group by username having count(*)>1)

select * from test

username             department           
-------------------- -------------------- 
Alwin                UUDI

(所影响的行数为 1 行)

------解决方案--------------------
探讨
出现完全相同的?

select distinct * into tmp from tb
truncate table tb
insert into tb select * from tmp

------解决方案--------------------
SQL code

create table test(username varchar(20),department varchar(20))
insert test
select 'roy','com' union all
select 'Alwin','UUDI' union all
select 'roy','com' 
go

--drop table test

delete test where username in (select username from test group by username,department having count(*)>1)

select * from test


username             department           
-------------------- -------------------- 
Alwin                UUDI

(所影响的行数为 1 行)

------解决方案--------------------
探讨
SQL codeselectdistinct*into #from tbgotruncatetable tbgoinsert tbselect*from #godroptable #