Solved

Remove Duplicate record using SQL Query

Posted on 2012-03-27
4
482 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
ID: 37774808
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
ID: 37775080
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
ID: 37778377
"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
ID: 37779806
yes, want to keep lower number bucket.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

912 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