Solved

How do i delete all duplicates and singlise a table

Posted on 2011-03-21
5
235 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

23 Experts available now in Live!

Get 1:1 Help Now