Solved

Delete Dupicate record in Access

Posted on 2013-11-20
15
411 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
  • 9
  • 4
  • 2
15 Comments
 
LVL 18

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 24

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 18

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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 24

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 24

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 24

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

820 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