View Full Version : Compare and Delete


FlyinBrian
05-01-2008, 06:29 AM
I'm trying to setup a delete query that cleans out the records in one table that are also in another table. I was trying to use the address field (would use name but one table splits it First and Last and the other doesn't) but I just can't seem to run the delete query when using a join

anyone have any ideas on how I could setup this delete query?

FlyinBrian
05-01-2008, 07:09 AM
er, wait. I misunderstood her instructions, I'm actually doing something somewhat simpler. I'm actually just looking through two files and finding duplicate records within the file. much more reasonable but still something I haven't really done before

I got this code from another DB she gave me:

In (SELECT [Fname CAPS] FROM [2003 2008] As Tmp GROUP BY [Fname CAPS], [Lname CAPS], [Address CAPS] HAVING Count(*)>1 And [Fname CAPS] = [heart].[Fname CAPS] And [Address CAPS] = [heart].[Address CAPS])

I think I get what it's doing in theory. It's searching for and putting any records together that share the same name and address. I'm not sure if I'm setting it up correctly though, one file gives me no results and the other file just spits out all the records when I try to put this code in the select criteria :(

jal
05-01-2008, 11:40 AM
er, wait. I misunderstood her instructions, I'm actually doing something somewhat simpler. I'm actually just looking through two files and finding duplicate records within the file. much more reasonable but still something I haven't really done before

I got this code from another DB she gave me:

In (SELECT [Fname CAPS] FROM [2003 2008] As Tmp GROUP BY [Fname CAPS], [Lname CAPS], [Address CAPS] HAVING Count(*)>1 And [Fname CAPS] = [heart].[Fname CAPS] And [Address CAPS] = [heart].[Address CAPS])

I think I get what it's doing in theory. It's searching for and putting any records together that share the same name and address. I'm not sure if I'm setting it up correctly though, one file gives me no results and the other file just spits out all the records when I try to put this code in the select criteria :(
This looks similar to a popular strategy for deleting dup records but I have trouble following it precisely - I'm a beginner. Let me give you the usual strategy and perhaps you can apply it to your tables.

Suppose you wanted to delete any repeating names from a table (i.e. John Smith should not occur twice). Have an autoNumber column called ID

DELETE FROM Customers where
ID Not in
(
Select min(ID) From Customers Group By FirstName, LastName

)

The inner query forms a group of John Smiths. Each John Smith in the group has an ID#. In that bunch of IDs, obviously one of them is the lowest (the Min(id). So we delete all John Smiths except for one of them (by convention, the one with the Min(ID).