I have a database that contains numerous amounts of duplicates, each have a unique ID
but their PermitID
and EncID
are the same. I need to remove all but the highest ID in the database.
The sql statement,
DELETE FROM tblInvoices
WHERE EncID = '0237' AND PermitID IN (
SELECT Max(ID) FROM tblInvoices Group BY PermitID)
deletes all of the records. I have tried
DELETE FROM tblInvoices
WHERE EncID = '0237' AND PermitID
< (SELECT Max(ID) FROM tblInvoices Group BY PermitID)
but I receive the error
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
An example of the data would be
ID PermitID EncID
1 11 22
2 11 22
3 11 22
4 12 23
5 12 23
I would want to keep 3, remove 2 and 1. I would also like to keep 5 and remove 4