Delete duplicate records

a7n9

Well-known Member
Joined
Sep 15, 2004
Messages
696
Hello all,

I want a SQL code to delete all the duplicate records but one where the record's field2 value is maximum.

For eg:
Field1 Field2
55 33
44 22
222 22
44 11
55 56
55 31

So if I run the query the final table should look like
Field1 Field2
44 22
222 22
55 56

I found SQL code to delete all the duplicate records but not where I can put a condition on the second field. Also, I'm very new at SQL so please help me.
Code:
DELETE *
FROM Table1
WHERE (((Table1.Field1) In (SELECT [Field1] FROM [Table1] As Tmp GROUP BY [Field1] HAVING Count(*)>1 )));

Edit 1: I'm checking the duplicate records for field1
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I'm sorry but I can't understand the logic of your result.

Is this not what you want?
Code:
Field1	Field2
222	22
55	56

I got that result with the following query:

DELETE Table1.Field1, Table1.Field2, *
FROM Table1
WHERE (((Table1.Field1) In (SELECT [Field1] FROM [Table1] As Tmp GROUP BY [Field1] HAVING Count(*)>1 )) AND ((Table1.Field2)<>DMax("[Field2]","Table1")));
 
Upvote 0
Thanks Norie for your reply. Probably I was not clear stating my problem. The query should delete all the duplicate records for field1 but leave one duplicate record where field2 value is maximum.
As given in my example:
Original Table:
Field1 Field2
55 33
44 22
222 22
44 11
55 56
55 31

Now here we can see that there 3 duplicate records of 55 and 2 duplicate records of 44. Now the query should keep the second last row because field2 value is maximum for the duplicate records of 55. Also, it should keep second row as the field2 value for the duplicate records of 44 is maximum on the second row. And rest duplicate records can be deleted. Therefore the result would be

Field1 Field2
44 22
222 22
55 56

Also, I ran your code but it's deleting all duplicate records. THanks for your help.
 
Upvote 0
Don't you just mean you want the maximum for field 2 for each value in field 1?
 
Upvote 0
Yes, that's what I mean and your code runs perfectly fine. My data type specification for field2 was wrong. It was kept as text but when I changed it to integer it runs fine.

Thanks a lot.
 
Upvote 0
Hey Norie,

I was trying your code one more time but when I ran it now it is deleting all the duplicate records. Here's my code. Can you please tell me what is the error?Thanks for your help.

Code:
Dim tbl_name_C As String, tbl_name_O As String
tbl_name_O = InputBox("Enter the name of the original table", "Original Table")
tbl_name_C = InputBox("Enter the name of the new duplicates table", "Duplicates Table")

Dim strSQL4 As String
'Create a copy of the original table
strSQL4 = "SELECT [ID Number],[Application Number] INTO " & tbl_name_C & " FROM " & tbl_name_O & ";"
MsgBox strSQL4
DoCmd.RunSQL strSQL4


Dim strSQL As String
'delete duplicates from the table
strSQL = "DELETE " & tbl_name_C & ".[ID Number]," & tbl_name_C & ".[Application Number],*" & _
" FROM " & tbl_name_C & _
" WHERE (((" & tbl_name_C & ".[ID Number]) In (SELECT [ID Number]" & _
" FROM [" & tbl_name_C & "] As Tmp GROUP BY [ID Number] HAVING Count(*)>1 ))" & _
" AND ((" & tbl_name_C & ".[Application Number])<>DMax(" & Chr(34) & "[Application Number]" & Chr(34) & "," & Chr(34) & tbl_name_C & Chr(34) & ")));"
MsgBox "" & strSQL
DoCmd.RunSQL strSQL
 
Upvote 0
Hi again,

I think I've spotted the error but cannot find a solution to it. See what your code doing is finding maximum from all of the records for the second field, but I want the maximum only for the duplicate records. I will explain my example again.

This is the original table
ID NUMBER Application NUmber
22 44
22 45
22 89
40 32
33 90
44 90
44 01

and the result table should be
ID NUMBER Application NUmber
22 89
40 32
33 90
44 90
 
Upvote 0
Does the following do what you need?

Code:
SELECT testtable.IDNumber, Max(testtable.ApplicationNumber) AS ApplicationNumber
FROM testtable
WHERE (testtable.IDNumber) In (SELECT [IDNumber]
FROM testtable GROUP BY [IDNumber] HAVING Count(*)>1)
GROUP BY testtable.IDNumber;

the following is what I get:
Book1
ABCDE
1TestTableBeforeTestTableAfter
2IDNumberApplicationNumberIDNumberApplicationNumber
31891270
47811690
58172275
612703358
712564037
812444471
91690
101643
112221
122239
132275
143338
153328
163358
173422
184037
194021
204469
214471
224454
Sheet1


I would also recommend you don't use Spaces in your field headings ie will be far better for coding AND easier to read without all those pesky '['s.

Regards,
 
Upvote 0
Thanks Ian. That will do. However, there is a problem. Now I want to get all the records from the original table for the those IDnumbers we got from your query into a new table.
For Ex:
Book1
ABCDEFGHIJ
1OriginalTableNewTable
2IDNumberApplicationNumberField3Field4Field5IDNumberApplicationNumberField3Field4Field5
3189dfr1270hhfd
4781ghh1690fdhfd
5817hhh2275fffd
61270hhfd3358QKU
71256fdhfdf4037JLB
81244dfhfd4471ZNZ
91690fdhfd
101643fhfh
112221fhfh
122239fffd
132275fffd
143338HHM
153328RCC
163358QKU
173422FPT
184037JLB
194021PXZ
204469EER
214471ZNZ
224454VIR
Sheet1

Also, tried getting all the fields while checking for the maximum condition but the query runs awfuly slow. Can you please help me with that?

Thanks.
 
Upvote 0
Can I ask what the purpose of this is?

Are trying to have a table with the most recent applications in?
Or am I way off the mark.
From what I can, sort of, see, I think you may be going about this the wrong way.

If you intent to repeat this process agian in the future then you shouldn't be creating a Table from a Table, as it's not a good idea to keep the same data in 2 places.

Can expand on what you need so we don't give you advice which may be ill advised.

As for your slow Query, are all for the fields indexed properly?
And how many records is this working with? Fields/Records

Edit Against my better judgment:

SELECT NewTable.IDNumber, NewTable.ApplicationNumber, OldTable.Field3, OldTable.Field4, OldTable.Field5 INTO NewTable2
FROM NewTable LEFT JOIN OldTable ON (NewTable.IDNumber = OldTable.IDNumber) AND (NewTable.ApplicationNumber = OldTable.ApplicationNumber);

Cheers,
 
Upvote 0

Forum statistics

Threads
1,221,841
Messages
6,162,314
Members
451,759
Latest member
damav78

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top