Nov 20 2021 08:12 AM
Hello, I have duplicates in my table and I want to delete only 1 of them. I have searched on the net for some code and found the below and fashioned it to my case. I have an error though and it appears to be syntax. Do you see where I the error is? Also, if there is better code I am grateful for assistance.
Note: I am a novice user of access and not a programmer.
Delete duplicate records in ACCESS 2010. | Toolbox Tech
DELETE *
FROM [Import_Excel_wRepeats]
WHERE [ID1] IN
(SELECT [ID1] FROM
(SELECT [ID1]
FROM [Import_Excel_wRepeats]
WHERE [Section] In
(SELECT [Section] FROM [Import_Excel_wRepeats] As Tmp
GROUP BY [Section], [short description]
HAVING Count(*) >1
And [short description] = [Import_Excel_wRepeats].[short description])
WHERE ID1 NOT IN
(SELECT FIRST(ID1)
FROM [Import_Excel_wRepeats]
GROUP BY [Section], [short description]
HAVING Count(*) >1)
);
Nov 20 2021 09:26 AM
SolutionHi
I'm just about to go out but have a look at the 2 example apps in the attached zip file which show two different approaches to this
Hope one/both are of help
Nov 20 2021 10:48 AM - edited Nov 20 2021 10:49 AM
very nice. works great! I also like it that there are 2 fields to test on. Thank you very much.
Nov 20 2021 01:46 PM
Nov 24 2021 12:51 AM
Nov 24 2021 12:56 AM
Nov 24 2021 02:41 PM
Nov 25 2021 01:27 AM
To use the method you described, you should use a unique index for the fields that should not contain duplicates. Your suggestion of a composite primary key isn't necessary
Nov 25 2021 09:56 AM - edited Nov 25 2021 09:57 AM
True, either way works...if you're only using one field to identify duplicates. If you have to combine several fields to determine a unique record then as far as I know you'd need to create a multi-field PK
Nov 25 2021 10:59 AM
Not true.
Setting a unique index works on a single field or multiple fields.
There is no need to set those fields as a primary key for this purpose.
In general, it is usually better to avoid the use of composite primary keys.
Nov 26 2021 09:41 AM
Nov 26 2021 10:09 AM - edited Nov 26 2021 11:50 AM
PMFJI:
You set a unique composite index like this in Access:
Of course, you could also include the multiple fields in a Composite Primary Key.
In my experience, the main reason NOT to use composite Primary Keys is the difficulty they present in multi-field queries. If you have to create joins on multiple fields, the SQL statement grows to an unwieldy and confusing state. I don't know if it impacts performance, though.
Nov 26 2021 01:27 PM - edited Nov 26 2021 01:28 PM
George has already shown you how to create a unique index for multiple fields.
I have used composite primary keys in the past but eventually stopped doing so (except in rare cases) because they tended to make managing queries & code more unwieldy. If you run an online search you will find articles both for & against their use. A fairly balanced (if rather complex) article can be found at https://medium.com/@pablodalloglio/7-reasons-not-to-use-composite-keys-1d5efd5ec2e5 though I've only skimmed the article
Lets take a simple reason for avoiding them. As well as not containing duplicates, primary keys cannot ever contain null values. So all those fields must ALWAYS contain data which at times may prevent a new record being saved. However, with a unique index, you can choose to ignore nulls.
Hope that helps
Nov 27 2021 09:51 AM
Nov 28 2021 10:15 AM
Nov 20 2021 09:26 AM
SolutionHi
I'm just about to go out but have a look at the 2 example apps in the attached zip file which show two different approaches to this
Hope one/both are of help