Solved

How do i delete all duplicates and singlise a table

Posted on 2011-03-21
5
226 Views
Last Modified: 2012-05-11
I currently have the following command which i use to to delete duplicates i.e.

delete * from [dbo].[commerce_manufacturer_staging]
where exists (
select *
from [dbo].commerce_manufacturer_staging as a
where a.[Name] = [dbo].commerce_manufacturer_staging.[Name]
and a.[TypeId] = [dbo].commerce_manufacturer_staging.[TypeId]
and a.[id] < [dbo].commerce_manufacturer_staging.[id]
)

Open in new window


However this take 20-30mins to run....

However If i run the following command

SELECT [Name],typeid, count([Name]) FROM 
commerce_manufacturer_staging 
GROUP BY [Name],typeid 
having count([Name]) > 1

Open in new window



This runs a lot quicker, however i cant work out how to delete the duplicates and only have one  instance of name and type per table. Can anyone tell me how i can use this a lot more efficently than what i have done intially?

Thanks
Webbo
0
Comment
Question by:Webbo_1980
  • 3
5 Comments
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 35185201
Try this approach:
http://weblogs.sqlteam.com/MLADENP/archive/2009/01/05/The-simplest-way-to-delete-duplicates-and-compare-two-result.aspx

If you need assistance let me know...glad to help you with the actual writting of the query.
0
 

Author Comment

by:Webbo_1980
ID: 35185342
thanks sl8rz this looks perfect however im struggling to adapt it to my example, any chance i could take you on your offer and ask for some guideance / example please?
0
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 35185362
sure.  Give me a few minutes.
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 35185416
try this.
;WITH cte 
     AS (SELECT [Name], 
                typeid, 
                ROW_NUMBER() 
                  OVER(PARTITION BY [Name],typeid ORDER BY id DESC) rn 
           FROM commerce_manufacturer_staging) 
DELETE FROM cte 
      WHERE rn <> 1

Open in new window

0
 
LVL 15

Expert Comment

by:David L. Hansen
ID: 35185541
Which version of SQL Server are you using?
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now