Loading…
When duplicates exist, you need a consistent rule to pick the best record.
Count how many important fields have data:
-- Completeness score = count of non-null fields
CASE WHEN phone IS NOT NULL THEN 1 ELSE 0 END +
CASE WHEN company IS NOT NULL THEN 1 ELSE 0 END
-- Score: 0 (worst) to 2 (best)
WITH ranked AS (
SELECT *,
ROW_NUMBER() OVER(
PARTITION BY LOWER(TRIM(email)) -- group duplicates
ORDER BY completeness DESC, id -- best first
) as rn
FROM leads
)
SELECT * FROM ranked WHERE rn = 1; -- keep #1 only
| Step | What It Does |
|---|---|
| PARTITION BY | Groups rows by cleaned email |
| ORDER BY ... DESC | Puts "best" record first |
| WHERE rn = 1 | Keeps only the winner |
For each unique email, keep the record with the most data:
Use a CTE to calculate and rank.
Downloading SQL engine… (one-time)
This runs entirely in your browser and is cached for next time.