Problems / Dedupe: Keep the Most Complete Row / Editorial
CASE WHEN name IS NOT NULL THEN 1 ELSE 0 END + ...
name
phone
company
(name IS NOT NULL)::INT + ...
ROW_NUMBER() OVER (PARTITION BY email ORDER BY completeness DESC, contact_id ASC)
contact_id ASC
rn = 1
email
Not all duplicates are equal. A naive DISTINCT cannot dedupe these rows (they differ in their NULL patterns), and keeping an arbitrary row may discard the one copy that has the phone number. Scoring completeness and ranking with ROW_NUMBER implements a survivorship rule — the record with the most information survives. Two details separate correct from lucky solutions: a deterministic tiebreaker (ties are real in merged CRM data), and *keeping* the winning row as-is rather than merging fields across rows, which is a different (and riskier) cleanup strategy.
DISTINCT
ROW_NUMBER
Solve Dedupe: Keep the Most Complete Row yourself →