Find duplicates in Postgres table and remove them

 To find all duplicates in table:

SELECT
    name,
    COUNT( name )
FROM
    client
GROUP BY
    name
HAVING
    COUNT( name )> 1
ORDER BY
    name;
To remove one:
DELETE
FROM
    client a
        USING client b
WHERE
    a.id < b.id
    AND a.name = b.name;

Comments

Popular posts from this blog

Certbot

Mapserver install on Ubuntu 18.04 with Naviserver: