Solved

Delete duplicates from SQL table based on dupe email addresses

Posted on 2009-07-15
4
296 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
ID: 24860498
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24860499
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
ID: 24860745
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
ID: 31603975
Thanks MartinCMS... I just added the Record_ID field and your statement worked great!
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

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…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

680 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