Solved

Remove Duplicate record using SQL Query

Posted on 2012-03-27
4
480 Views
Last Modified: 2012-04-05
Hi,

I have a sql table where I want to remove duplicate records. Without using temp table, is there a better way to remove duplicates.

I want to remove emp1 from bucket 12 & wants to keep one emp1 in bucket 12. Also I want to remove emp2 from bucket 16 as emp2 exists in bucket12.

Here is my table

Table1
-----------
   bucketNum    empName
      12           emp1
      12           emp2
      12           emp3
      12           emp1
      16           emp2
      16           emp4


Output
------------
 bucketNum    empName    
      12           emp1
      12           emp2
      12           emp3    
      16           emp4


Thanks
0
Comment
Question by:r_pat72
4 Comments
 
LVL 39

Accepted Solution

by:
appari earned 500 total points
Comment Utility
try this, take back up of existing data or run it within a transaction and commit after checking the results.
;with data as (Select *, row_number() over(partition by empName order by bucketNum) rowNum from tableName)
delete from data where rowNum > 1

Open in new window

0
 
LVL 20

Expert Comment

by:BuggyCoder
Comment Utility
here is the list of all possible ways of removing duplicates:-
http://www.simple-talk.com/sql/t-sql-programming/removing-duplicates-from-a-table-in-sql-server/
0
 
LVL 2

Expert Comment

by:ben9
Comment Utility
"remove emp2 from bucket 16 as emp2 exists in bucket12."

What is your criteria for which one to keep? Do you always keep the lower numbered bucket and consider the others duplicates?
0
 

Author Comment

by:r_pat72
Comment Utility
yes, want to keep lower number bucket.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
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…

762 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

16 Experts available now in Live!

Get 1:1 Help Now