So, I am trying to clear phone records in a database table.
I learned how to find exact matches in two fields using:
SELECT `First Name`, `Last Name`, COUNT(*) c FROM phone.contacts GROUP BY `Last Name`, `First Name` HAVING c > 1;
Wow, great.
I want to expand it to look at numerous fields, to see if the phone number in 1 of 3 phone fields is a duplicate.
So, I want to check 3 fields ( general mobile , general phone , business phone ).
1. see that they are not empty ('') 2. see if data (number) is displayed in any of the two other telephone fields at any point in the table.
Thus, pushing my restricted SQL beyond, I came up with the following, which seems to return records with three empty phone fields, as well as records that do not have duplicate phone numbers.
SELECT id, `first name`, `last name`, `general mobile`, `general phone`, `general email`, `business phone`, COUNT(CASE WHEN `general mobile` <> '' THEN 1 ELSE NULL END) as gen_mob, COUNT(CASE WHEN `general phone` <> '' THEN 1 ELSE NULL END) as gen_phone, COUNT(CASE WHEN `business phone` <> '' THEN 1 ELSE NULL END) as bus_phone FROM phone.contacts GROUP BY `general mobile`, `general phone`, `business phone` HAVING gen_mob > 1 OR gen_phone > 1 OR bus_phone > 1;
It is clear that my logic is wrong, and I wondered if anyone could point me in the right direction / have mercy, etc ....
Thank you very much