Gurpreet runs analytics for a Ludhiana-based tractor-parts distributor, and one Monday the sales head asks why the dealer count in the dashboard is 1,412 when the field team insists they have about 1,300. Gurpreet looks, and finds the dealer “Kaur Auto Spares” four times.

Four rows. Same phone number. One says “Kaur Auto Spares”, one “Kaur Auto Spares Pvt Ltd”, one has a typo, “Kuar Auto Spares”. The fourth is identical to the first, down to the timestamp. His first instinct is the one most of us have: remove duplicates. But that single phrase hides three different problems, and the dealer count will be wrong in a different way depending on which one he solves.

Problem one: the exact repeat

The fourth row is a true duplicate: every column matches. It almost certainly came from a retried file load or a job that ran twice. Here, which row survives does not matter, because they are the same. You keep one and discard the rest. The more important question is why it happened, since if the load job is not safe to repeat, the problem will return next month. A fix that only cleans up after the job leaves the cause in place.

Problem two: same entity, different versions

Now the first and second rows. Perhaps the dealer was entered in March and updated in June when the company converted to a private limited. They share an ID in the source system, but the extract kept every version. Which one is right? Usually the latest, but “latest” needs a column to decide it. A last-modified timestamp is best. A load date is weaker, because it tells you when you received the row, not when the change happened. A row number from the source is weakest of all.

The standard technique is to rank the rows within each key, ordered by the timestamp from newest to oldest, and keep rank one. Window functions such as ROW_NUMBER do this in a few lines. What nobody tells you is the tie. If two versions carry the same timestamp, the database picks one arbitrarily, and it may pick differently tomorrow. Always add a second sort column that breaks ties, such as a source sequence number, so that the survivor is the same every run. A pipeline that gives different answers on identical input is much worse than one that is merely imperfect.

Sometimes, though, you do not want to throw the old version away. If you later need to know what the dealer was called when an invoice was raised in March, keep history, as slowly changing dimensions do, and deduplicate only for the “current” view.

Problem three: no shared key at all

The “Kuar” row is the hard one. It came from a different system, a field-sales app, where dealers are typed in by hand. There is no shared ID. The only clues are name, phone and town. This is entity resolution, and it is a matching problem, not a deletion problem.

You can match on phone number, with cleaning first: strip spaces, drop a leading +91 or 0, keep ten digits. You can compare names after normalising case, punctuation and suffixes like “Pvt Ltd”. For typos, fuzzy measures such as edit distance help. Each approach fails differently. Phone numbers are shared by two brothers running separate shops. Names are shared by unrelated businesses in different towns. Fuzzy matching with a loose threshold will cheerfully merge “Sri Balaji Traders” in Madurai with “Sri Balaji Trading Co” in Nashik.

So the sensible approach has tiers. Exact match on a cleaned phone plus town: merge automatically. Strong similarity on name with the same town but a different phone: send to a human review list. Everything else: leave alone. Wrongly merging two customers is usually worse than leaving a duplicate, because a merge destroys information and a leftover duplicate merely inflates a count. Which error is more costly depends on use, though. For a mailing list, a duplicate means a customer gets two letters. For credit limits, a wrong merge could combine two dealers’ exposure.

Back to the Monday meeting

Gurpreet decides to report the work in the order he did it. After removing exact repeats, rows fall from 1,412 to 1,371. After keeping the latest version per dealer ID, to 1,326. After tiered matching across the two systems, 1,305 dealers, with 22 pairs sitting in a review list. He tells the sales head the answer is “between 1,305 and 1,327”, depending on how the 22 are resolved. Nobody loves a range. But it is accurate, and the field team’s “about 1,300” turns out to be fair.

He also writes down the rule: survivor is the row with the latest modified time, ties broken by source sequence, and the removed rows are copied to an audit table rather than deleted. Six months later, when a dealer complains that an old address was used on an invoice, that audit table is the only reason anyone can explain what happened.

The next time a dataset “has duplicates”, ask three questions before deleting anything. Is this a repeat, a version or a different record that looks alike? Which column decides the survivor, and does it break ties? Where do the discarded rows go?

Figures and names in the examples are illustrative.

Leave a Reply

Trending