Home All Groups Group Topic Archive Search About

delete duplicate items?

Author
4 Aug 2006 10:48 PM
Qnguyen
Does anyone know how to view and delete identical items on table?
What is SQL command to do this. Thanks

Author
4 Aug 2006 10:55 PM
Arnie Rowland
Here are a couple of resources that may guide you.

http://www.aspfaq.com/2431
http://www.sql-server-performance.com/rd_delete_duplicates.asp

--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous


Show quote
"Qnguyen" <Qngu***@discussions.microsoft.com> wrote in message
news:D86F92E6-7057-4FE0-9190-631F76DC896B@microsoft.com...
> Does anyone know how to view and delete identical items on table?
> What is SQL command to do this. Thanks
Author
5 Aug 2006 7:22 PM
--CELKO--
>> Does anyone know how to view and delete identical items on table? <<

Why do you have redundant duplicates at all?  The PRIMARY KEY should
have prevented the problem.

If we show you how to clean up your mess, then will you promise to add
a real key to the table?  Mop the floor then fix the damn leak!
Author
5 Aug 2006 8:59 PM
Tony Rogerson
Perhaps they are importing data from multiple systems.

Perhaps they are deduplicating address information.

sh*t man - you really ought to get out of the class room and get a job and
some experience....

--
Tony Rogerson
SQL Server MVP
http://sqlblogcasts.com/blogs/tonyrogerson - technical commentary from a SQL
Server Consultant
http://sqlserverfaq.com - free video tutorials


Show quote
"--CELKO--" <jcelko***@earthlink.net> wrote in message
news:1154805763.355731.320590@h48g2000cwc.googlegroups.com...
>>> Does anyone know how to view and delete identical items on table? <<
>
> Why do you have redundant duplicates at all?  The PRIMARY KEY should
> have prevented the problem.
>
> If we show you how to clean up your mess, then will you promise to add
> a real key to the table?  Mop the floor then fix the damn leak!
>
Author
7 Aug 2006 8:56 AM
Locky
Qnguyen wrote:
> Does anyone know how to view and delete identical items on table?
> What is SQL command to do this. Thanks

set nocount on
declare @id int,@rc int
create table #WithDup (id int)
insert into #WithDup(id) select id from sysobjects
insert into #WithDup(id) select id from sysobjects

select top 10 id,count(*) from #withDup group by id having(count(*))>1
order by 2 desc

declare cur cursor local static forward_only for
select id,count(*)-1 from #WithDup group by id having(count(*))>1

open cur
fetch next from cur into @id,@rc
while @@fetch_status = 0 begin
    set rowcount @rc
    delete #WithDup where id = @id
    set rowcount 0
fetch next from cur into @id,@rc
end
deallocate cur

select top 100 id,count(*) from #withDup group by id having(count(*))>1
order by 2 desc

drop table #withdup
Author
7 Aug 2006 9:16 AM
jsfromynr
Hi There,

You may like to try these methods.

1. Add an idenity Column to the table
Allter table yourtable Add myseq int identity(1,1)
2. Delete all the rows where rows are duplicate based on the condition

Delete from yourTable where Exists ( Select 1 from YourTable B where
Yourtable.column1=B.column1
and Yourtable.column2=B.column2
and Yourtable.myseq <B.myseq
)

Or simple

1. Select distinct * into newTable from YourTable
2. delete from Yourtable
3. insert into Yourtable select * from newTable

I hope this helps

With Warm regards
Jatinder Singh
http://jatindersingh.blogspot.com
http://sqloracle.tripod.com
Author
8 Aug 2006 9:31 AM
Petrisor Dumitru
or
1. create #NewTable
2. create unique index Name_index on #NewTable(column or columns)
3 insert #NewTable select YourTable with IGNORE_DUP_KEY
4 delete from YourTable
5 insert into Yourtable select * from #NewTable

but I preffer jsfromynr solution...

AddThis Social Bookmark Button