Solved

How do i delete all duplicates and singlise a table

Posted on 2011-03-21
5
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 41

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

732 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