I have a Customers table with a columns called CustomerID, CustomerNumber. I have an Orders table that references the CustomerID table in Customers.
I need to find and remove all duplicate records for CustomerNumber that DO NOT have orders in the Orders table.
This is how I am finding my duplicates in the Customers but I do not know how to filter out by the orders table then remove the extra records:
SELECT Name, CustomerNumber, COUNT(*) As DupeCount
FROM StagingCustomers
WHERE ManufacturerID=15
GROUP BY Name, CustomerNumber
HAVING COUNT(CustomerNumber) > 1
ORDER BY CustomerNumber
Add an
EXISTSclause to check for any records without a relation:EDIT:
Example of how to delete records below. This will set
IsDeletedto 1 for every record that meets the above criteria, AND is not the lowestcustomeridperCustomerNumber. For a clearer answer you need to give your table layout and relations.