Sorry for the delay in reply. I have been a little busy the last couple of days.
Below my answer is based on the following assumptions:
1) You will fix problems with the spelling of the name using some other mechanism (you mentioned that you clean it with regular expressions in the comments to the original question).
2) The DUP set can be identified using First Name, Lastname and Birth (you mentioned this in a comment on the original question).
3) Name, Surname and Birth cannot be NULL.
4) You cannot have more than one FIN entry in a DUP set (you mentioned this in a comment on the original question).
If any of the above assumptions is invalid, my answer should be changed.
The following steps are listed below:
1) Update all FIN records to copy the PersonalKey file from a record other than FIN:
UPDATE Pruebas p1 INNER JOIN Pruebas p2 ON p1.Firstname = p2.Firstname AND p1.Lastname = p2.Lastname AND p1.Birth = p2.Birth SET p1.PersonalKey = p2.PersonalKey WHERE p1.Source like 'FIN%' AND p1.PersonalKey is null AND p2.PersonalKey is not null;
2) Delete all entries other than FIN, where we have the FIN entry:
DELETE p2 FROM Pruebas p1 INNER JOIN Pruebas p2 ON p1.Firstname = p2.Firstname AND p1.Lastname = p2.Lastname AND p1.Birth = p2.Birth WHERE p1.Source like 'FIN%' AND p2.Source not like 'FIN%';
At this point, all DUPs with the FIN entry have been cleared, so that only the FIN entry will remain.
3) If we decide to stay with a DRV record for all other cases. We will need to copy PersonalKey from another record to a DRV record:
UPDATE Pruebas p1 INNER JOIN Pruebas p2 ON p1.Firstname = p2.Firstname AND p1.Lastname = p2.Lastname AND p1.Birth = p2.Birth SET p1.PersonalKey = p2.PersonalKey WHERE p1.Source like 'DRV%' AND p1.PersonalKey is null AND p2.PersonalKey is not null;
4) Delete all entries other than DRV, where we have a DRV entry:
DELETE p2 FROM Pruebas p1 INNER JOIN Pruebas p2 ON p1.Firstname = p2.Firstname AND p1.Lastname = p2.Lastname AND p1.Birth = p2.Birth WHERE p1.Source like 'DRV%' AND p2.Source not like 'DRV%';
At this point, all the DUPs with the DRV record have been cleared, so that only the DRV record remains.
If the only record type is SA, then more DUP is left, and we are done.
5) If we want to select the record with the most information filled out or if we have finished 3 and 4, and there are more than one type of records that cause DUP. We need to copy the PersonalKey from any record in the DUP collection that has it for any record that does not have it for all records other than FIN:
UPDATE Pruebas p1 INNER JOIN Pruebas p2 ON p1.Firstname = p2.Firstname AND p1.Lastname = p2.Lastname AND p1.Birth = p2.Birth SET p1.PersonalKey = p2.PersonalKey WHERE p1.Source not like 'FIN%' AND p1.PersonalKey is null AND p2.PersonalKey is not null;
6) Delete all entries except the one that has the most information (as determined by the calculated info_score column):
DELETE p5 FROM Pruebas p5 INNER JOIN (SELECT p3.Firstname , p3.Lastname , p3.Birth , MIN(p3.Id) AS min_id FROM Pruebas p3 INNER JOIN (SELECT p1.Firstname , p1.Lastname , p1.Birth , count(*) AS c , MAX((p1.Name is not null) + (p1.RFC is not null) + (p1.Source is not null) + (p1.PersonalKey is not null)) AS info_score FROM Pruebas p1 GROUP BY p1.Firstname , p1.Lastname , p1.Birth HAVING count(*) > 1) p2 ON p3.Firstname = p2.Firstname AND p3.Lastname = p2.Lastname AND p3.Birth = p2.Birth AND ((p3.Name is not null) + (p3.RFC is not null) + (p3.Source is not null) + (p3.PersonalKey is not null)) = p2.info_score GROUP BY p3.Firstname , p3.Lastname , p3.Birth) p4 ON p4.Firstname = p5.Firstname AND p4.Lastname = p5.Lastname AND p4.Birth = p5.Birth AND p4.min_id <> p5.Id;
At this point, all DUPs were minimized while retaining PersonalKey if it was available, and FIN records were saved if they existed differently, or a DRV record or the record with the most information was saved.
Let me know if you have any questions with any of the above questions.
Hope this helps,
-Dipin