0

I have a table which contains duplicates and I would like to keep only one row for each duplicates.

I can select duplicates with my SQL command :

SELECT DISTINCT ON (email, first_name, last_name) * from customer;

But I would like to use DELETE with my previous command.

This command should work right ?

DELETE FROM customer WHERE customer.id NOT IN 
    (SELECT id FROM 
        (SELECT DISTINCT ON (email, first_name, last_name) * from customer));

Is it true ?

2 Answers 2

2

I guess you have a id field.

delete from customer 
where id not in (
    select min(id)
    from customer
    group by email, first_name, last_name
)

The subquery finds the id of the rows you want to keep. Then you delete the other rows

Sign up to request clarification or add additional context in comments.

1 Comment

Yes it works fine ! My query didn't work because I need to add an alias I assume. Thank you anyway ! :)
1
  1. I can't find your ID in (SELECT DISTINCT ON (email, first_name, last_name) * from customer));
  2. The distinct on only return the first row of the duplication data that is unpredictable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.