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
Solved

Delete duplicates from SQL table based on dupe email addresses

Posted on 2009-07-15
4
295 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

790 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