Solved

Delete duplicates from SQL table based on dupe email addresses

Posted on 2009-07-15
4
293 Views
Last Modified: 2012-05-07
I have an SQL table that has a little more than 1,000 records. When I use 'distinct *' to select distinct records into a new table, and then look at email addresses, there are still duplicate records! Why is this?

Can anyone tell me how to delete rows of table data based on dupe email addresses? if I can kill the dupes by looking at the email addresses this would fix my problem. My email address field is called EmailAddr.....and there's no defined primary key on this table. Thanks...
0
Comment
Question by:jazjef
4 Comments
 
LVL 8

Expert Comment

by:vsudip
Comment Utility
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
Comment Utility
Seems like you have differebbt data in one of the other columns
for example

Col 1   col2
a         b
a        c

if you select DISTINCT * from the above table it still returns 2 records

http://sequelserver.blogspot.com/2008/02/deleting-duplicate-records.html
0
 
LVL 8

Accepted Solution

by:
MartinCMS earned 500 total points
Comment Utility
Try this query

DELETE FROM yourTable
WHERE EXISTS (SELECT 1
            FROM   yourTable AS yt2
            WHERE  yt2.EmailAddr= yourTable.EmailAddrAND
                     yt2.Record_ID < yourTable.Record_ID)

*** Record_ID - could be the record sequence field in your table
0
 
LVL 4

Author Closing Comment

by:jazjef
Comment Utility
Thanks MartinCMS... I just added the Record_ID field and your statement worked great!
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

728 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

10 Experts available now in Live!

Get 1:1 Help Now