Solved

Delete Dupicate record in Access

Posted on 2013-11-20
15
430 Views
Last Modified: 2013-11-27
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
0
Comment
Question by:skull52
[X]
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
  • 9
  • 4
  • 2
15 Comments
 
LVL 19

Expert Comment

by:Richard Daneke
ID: 39664275
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
 
LVL 25

Assisted Solution

by:chaau
chaau earned 500 total points
ID: 39664416
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
 
LVL 19

Expert Comment

by:Richard Daneke
ID: 39664604
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 25

Expert Comment

by:chaau
ID: 39664613
@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
 

Author Comment

by:skull52
ID: 39665954
DoDahD, chaau is correct if i attempt to convert the SELECT to a DELETE statement it wants to delete all 6 records.
0
 

Author Comment

by:skull52
ID: 39665970
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
 

Author Comment

by:skull52
ID: 39666072
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
 

Author Comment

by:skull52
ID: 39666083
even selecting a as the table it errors with "could not delete form the specified table"
0
 
LVL 25

Expert Comment

by:chaau
ID: 39667253
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
 

Author Comment

by:skull52
ID: 39667417
Nope, same issue "could not delete form the specified table"
0
 
LVL 25

Expert Comment

by:chaau
ID: 39667440
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
 

Author Comment

by:skull52
ID: 39668788
Nope... that throws a new error, see attached file
ErrorMsg.jpg
0
 

Author Comment

by:skull52
ID: 39668797
It seems to me it should not be this difficult to delete duplicate records.... Much simpler in MSSQL.
0
 

Accepted Solution

by:
skull52 earned 0 total points
ID: 39668861
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
 

Author Closing Comment

by:skull52
ID: 39680230
Mine worked
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

705 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