Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Syntax Question

Status
Not open for further replies.

moepower

Programmer
Oct 5, 2000
93
US
I have a table with three columns (first, last, and email). How do I write a DELETE command to find multiple records with the same email address and delete all but one.

ie.

John, Smith, JS@hotmail.com
John, Smiths, JS@hotmail.com

I want to key only on the email address and delete until only a single occurance of email.

Thakns,
 
You might try something like this (substituting your table
name for 'temp')...

delete from temp t1
where rowid <> (select max(rowid)
from temp
where email = t1.email)

Good luck,

Bob Bob Lowell
ljspop@yahoo.com

 
Be careful, as this will delete ALL rows in the table but one. I suppose you're trying to do that:

DELETE FROM temp t
WHERE rowid <> (select max(rowid)
from temp
where t.email = 'email')
and t.email = 'email';
 
Thanks, That's exactly what I'm looking for.
 
Hi,

Hey Kenrae, try my query again. The subquery
looks for the maximum rowid for this particular
email address (&quot;where email = t1.email&quot;) and
will not delete the row if the rowid is the
biggest rowid for _that_ email address. This
allows you to eliminate all duplicates in one
pass, rather than having to do one at a time.

Give it a shot...

Bob Bob Lowell
ljspop@yahoo.com

 
Eh, um, you're right. I didn't noticed the &quot;t1.&quot; in the subquery :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top