Identifying and removing duplicates
Relational databases work on the idea that items of data can be uniquely identified. However hard we try, there will always be bad data arriving from somewhere. This recipe shows you how to diagnose that and clean up the mess.
Getting ready
Let's start by looking at an example table, cust
. It has a duplicate value in customerid
:
CREATE TABLE cust ( customerid BIGINT NOT NULL ,firstname TEXT NOT NULL ,lastname TEXT NOT NULL ,age INTEGER NOT NULL); INSERT INTO cust VALUES (1, 'Philip', 'Marlowe', 33); INSERT INTO cust VALUES (2, 'Richard', 'Hannay', 37); INSERT INTO cust VALUES (3, 'Harry', 'Palmer', 36); INSERT INTO cust VALUES (4, 'Rick', 'Deckard', 4); INSERT INTO cust VALUES (4, 'Roy', 'Batty', 41); postgres=# SELECT * FROM cust ORDER...