Delete Dupicate record in Access

I have a dealer registration table that I have some duplicate records and I want to just delete the duplicates, I have created a find duplicate record query  
SELECT First(dbo_dealer_trade_expo_Bkup.[accountnumber]) AS [accountnumber Field]
FROM dbo_dealer_trade_expo_Bkup
GROUP BY dbo_dealer_trade_expo_Bkup.[accountnumber], dbo_dealer_trade_expo_Bkup.[businessname]
HAVING (((Count(dbo_dealer_trade_expo_Bkup.[accountnumber]))>1) AND ((Count(dbo_dealer_trade_expo_Bkup.[businessname]))>1));

Open in new window

which shows me the 3 records that have duplicates I created a 2nd Query to delete the duplicates using the first query in the Criteria field with the IN predicate
SELECT dbo_dealer_trade_expo_Bkup.accountnumber
FROM dbo_dealer_trade_expo_Bkup
WHERE (((dbo_dealer_trade_expo_Bkup.accountnumber) In (SELECT First(dbo_dealer_trade_expo_Bkup.[accountnumber]) AS [accountnumber Field]
FROM dbo_dealer_trade_expo_Bkup
GROUP BY dbo_dealer_trade_expo_Bkup.[accountnumber], dbo_dealer_trade_expo_Bkup.[businessname]
HAVING (((Count(dbo_dealer_trade_expo_Bkup.[accountnumber]))>1) AND ((Count(dbo_dealer_trade_expo_Bkup.[businessname]))>1));)));

Open in new window

and I run a select statement which returns all 6 records not just the 3 the first Query does
skull52Asked:
Who is Participating?
 
skull52Author Commented:
OK,
I took chaau suggestion and simplified it a little to this
DELETE *
FROM dbo_dealer_trade_expo_Bkup AS T1
WHERE ID <>
   (SELECT MIN(ID)
    FROM dbo_dealer_trade_expo_Bkup AS T2
    WHERE T2.accountnumber = T1.accountnumber);

Open in new window

and that worked perfectly, the key was having a unique ID column which was not in the original table. I am going to award the points to chaau because he lead me to the solution.
0
 
Richard DanekeTrainerCommented:
SQL syntax for a delete is DELETE not SELECT.    Changing the initial verb in your SQL statement will delete the records found.   Are you sure you want to delete the recordset displayed by this SQL statement?
0
 
chaauCommented:
I glad that you did not convert the select statement to delete statement straight away, as suggested DoDahD. Doing so would have deleted all 6 records. It is always a good idea to first run the Select statement and then convert it to delete statement.

Now, to your problem. If you need to delete just duplicate entries leaving one set in the database you need first to figure a field that is different between the duplicate entries. To figure what field is different you need to run this query:
SELECT *
FROM dbo_dealer_trade_expo_Bkup
WHERE (((dbo_dealer_trade_expo_Bkup.accountnumber) In (SELECT First(dbo_dealer_trade_expo_Bkup.[accountnumber]) AS [accountnumber Field]
FROM dbo_dealer_trade_expo_Bkup
GROUP BY dbo_dealer_trade_expo_Bkup.[accountnumber], dbo_dealer_trade_expo_Bkup.[businessname]
HAVING (((Count(dbo_dealer_trade_expo_Bkup.[accountnumber]))>1) AND ((Count(dbo_dealer_trade_expo_Bkup.[businessname]))>1)))));

Open in new window

See how I used "SELECT *"? This will return all columns. There must be a column that will stand out straight away. It could be a column called "ID", or "DateCreated" or something. When you identified this column you need to choose what entry to keep. Most of the time the entry with the latest value is considered "good". In this case you need to modify your select statement like this:
SELECT *
FROM dbo_dealer_trade_expo_Bkup a
INNER JOIN
(SELECT MAX(ID) AS goodValue, MIN(ID) AS badValue,
First(dbo_dealer_trade_expo_Bkup.[accountnumber]) AS [accountnumber Field]
FROM dbo_dealer_trade_expo_Bkup
GROUP BY dbo_dealer_trade_expo_Bkup.[accountnumber], dbo_dealer_trade_expo_Bkup.[businessname]
HAVING Count(dbo_dealer_trade_expo_Bkup.[accountnumber])>1 AND Count(dbo_dealer_trade_expo_Bkup.[businessname])>1) b 
ON a.accountnumber = b.[accountnumber Field] and a.ID = b.goodValue;

Open in new window

(you need to replace the column ID in the statement above to your column you have identified before)
The statement will show you only the three rows that are "good". To retrieve the "bad" rows, replace the last condition to a.ID = b.badValue and run the Select statement again.
Once you are happy with what you see (I mean you see only the "bad" values), convert the select statement to the delete statement, like this:
DELETE 
FROM dbo_dealer_trade_expo_Bkup a
INNER JOIN
(SELECT MAX(ID) AS goodValue, MIN(ID) AS badValue,
First(dbo_dealer_trade_expo_Bkup.[accountnumber]) AS [accountnumber Field]
FROM dbo_dealer_trade_expo_Bkup
GROUP BY dbo_dealer_trade_expo_Bkup.[accountnumber], dbo_dealer_trade_expo_Bkup.[businessname]
HAVING Count(dbo_dealer_trade_expo_Bkup.[accountnumber])>1 AND Count(dbo_dealer_trade_expo_Bkup.[businessname])>1) b 
ON a.accountnumber = b.[accountnumber Field] and a.ID = b.badValue;

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Richard DanekeTrainerCommented:
chaau, You are more elegant in your solution, but the fact remains if you are content with the 3 records from the first select, One can edit the statement replacing Select with Delete and the correct 3 records are removed.  The second SQL statement, therefore is not needed.
0
 
chaauCommented:
@DoDahD: not every SELECT statement can be converted to Delete. For example the first statement from the very first cannot be converted (there will be an error), and if the second statement is converted to delete - all 6 rows will be deleted. Therefore I have asked the OP not to do so.
0
 
skull52Author Commented:
DoDahD, chaau is correct if i attempt to convert the SELECT to a DELETE statement it wants to delete all 6 records.
0
 
skull52Author Commented:
chaau, the problem i am having is that 1) there is no ID column in that table and 2) the duplicates are exactly the same in all values. I tried your suggestion  (you need to replace the column ID in the statement above to your column you have identified before) and replaced the ID with the accountnumber and it again returned all 6 rows.
0
 
skull52Author Commented:
OK I added an ID column to the table and the SELECT statement worked and gave me the proper rows to delete, however the delete statement gave the following error "Specify the table containing the records you want to delete"
0
 
skull52Author Commented:
even selecting a as the table it errors with "could not delete form the specified table"
0
 
chaauCommented:
I think Access wants at least one column in the delete statement. Also, Access wants DISTINCTROW to be added to the delete statement. Try this:
DELETE DISTINCTROW dbo_dealer_trade_expo_Bkup.*
FROM dbo_dealer_trade_expo_Bkup
INNER JOIN
(SELECT MAX(ID) AS goodValue, MIN(ID) AS badValue,
First(dbo_dealer_trade_expo_Bkup.[accountnumber]) AS [accountnumber Field]
FROM dbo_dealer_trade_expo_Bkup
GROUP BY dbo_dealer_trade_expo_Bkup.[accountnumber], dbo_dealer_trade_expo_Bkup.[businessname]
HAVING Count(dbo_dealer_trade_expo_Bkup.[accountnumber])>1 AND Count(dbo_dealer_trade_expo_Bkup.[businessname])>1) b 
ON dbo_dealer_trade_expo_Bkup.accountnumber = b.[accountnumber Field] and dbo_dealer_trade_expo_Bkup.ID = b.badValue;

Open in new window

0
 
skull52Author Commented:
Nope, same issue "could not delete form the specified table"
0
 
chaauCommented:
OK, then Access is confusing with all the joins. Use this as a last resort:
DELETE DISTINCTROW dbo_dealer_trade_expo_Bkup.*
FROM dbo_dealer_trade_expo_Bkup
WHERE ID IN
(SELECT dbo_dealer_trade_expo_Bkup.ID FROM
INNER JOIN
(SELECT MAX(ID) AS goodValue, MIN(ID) AS badValue,
First(dbo_dealer_trade_expo_Bkup.[accountnumber]) AS [accountnumber Field]
FROM dbo_dealer_trade_expo_Bkup
GROUP BY dbo_dealer_trade_expo_Bkup.[accountnumber], dbo_dealer_trade_expo_Bkup.[businessname]
HAVING Count(dbo_dealer_trade_expo_Bkup.[accountnumber])>1 AND Count(dbo_dealer_trade_expo_Bkup.[businessname])>1) b 
ON dbo_dealer_trade_expo_Bkup.accountnumber = b.[accountnumber Field] and dbo_dealer_trade_expo_Bkup.ID = b.badValue);

Open in new window

0
 
skull52Author Commented:
Nope... that throws a new error, see attached file
ErrorMsg.jpg
0
 
skull52Author Commented:
It seems to me it should not be this difficult to delete duplicate records.... Much simpler in MSSQL.
0
 
skull52Author Commented:
Mine worked
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.