Website Designers and Webmasters

Dedicated to all the tasks Webmasters, Website Developers and Website Designers find themselves facing.

By: Ashton Sanders

SQL – Delete Similar or Identical Records

Oct 20 2009

Filed under: SQL,Webmaster

Deleting similar or identical records in SQL is a lifesaver. I found Brian Cryer’s “SQL How To…” about deleting similar or identical records in SQL. It’s got a lot of information, including the code for MS-Access SQL.

Here’s a quick explanation of what the following code does:

It searches MyTable for and deletes all rows where the dupField is the same except for the row with the lowest uniqueField.

Delete Similar Records

To delete similar records, i.e. where the records are not the same but one field is the same and only one copy needs to be preserved, try the following SQL:

DELETE T1
FROMÂ MyTable T1, MyTable T2
WHEREÂ T1.dupField = T2.dupField
AND T1.uniqueField > T2.uniqueField

To break this down even further, this code would delete all rows in MyTable that had the same value in dupField.

DELETET1
FROM MyTable T1, MyTable T2
WHERE T1.dupField = T2.dupField

This last line restricts the deleting to only rows that are also greater than another row with the same dupField

AND T1.uniqueField > T2.uniqueField

Obviously, you would replace the field and table names to work for your table.

Talk soon,
Ashton Sanders

By: Ashton Sanders

SQL Tutorial – Not In

Apr 23 2007

Filed under: SQL

I have an extremely complex SQL database system set up for an email newsletter broadcaster. This piece of code for SQL is what I’ve been looking for for the last week. Here you go:

AND emailaddr NOT IN(SELECT item FROM table WHERE patindex(‘%@%’,emailaddr) > 0 )

That’s all I’m going to say about it right now, look for more SQL Tutorials coming soon.

-Ashton Sanders

RSS

Where Am I?

You have found the semi-coherent ramblings of Ashton Sanders: a website designer, developer and webmaster. This is primarily Ashton's place to save notes about techniques and things that he learns in his never-ending conquest of the internet. Hopefully it's coherent enough to be useful to you too.

Subscribe:

Enter your email to get automatic emails whenever Ashton posts on the blog.

Email:

Tags and Categories

Links

Blog Roll