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
 
Thanks again, Ian. Yes, you are quite close it is for maximum applications by a student and it is going to be the recent one.

And about creating a new table, I cannot help it that's what I've been asked to do to create a new table.

No, not a single field is indexed. and I cannot do it manually cause there will be many tables like this one. and this task has to be done on a daily/weekly basis.

Right now I've over 8000 records and more than 120 fields and it's gonna grow, not the fields but the records.

The approach I'm taking now, you might find it funny. But it is working fast and I understand what's going on.
1. Create a copy of the original table in a tmptbl1
2. Copy all the duplicates in another tmptbl2
3. Keep only the maximum application records in tmptbl2
4. Delete all the duplicates from the first tmptbl1
5. Append the records got from action 3 into tmptbl1

Also, spaces in the fields are coming from other department, so cannot help it too.

If you have any other suggestion please let me know. Thanks for your help.
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Forum statistics

Threads
1,221,840
Messages
6,162,311
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