0

I have the following table:

Id color size 1 blue medium 2 blue medium 3 blue small 4 red big 

I to exclude all rows that have the color and size duplicated, but different Id. But bear in mind that I don't want to keep one of them, I want to exclude both that have color and size similar, but Id different. So this would be my final table:

Id color size 3 blue small 4 red big 

I found this post Delete Duplicate Records in PostgreSQL and this one https://www.postgresqltutorial.com/how-to-delete-duplicate-rows-in-postgresql/. But both of them want to keep one of the rows. I don't want it.

Is there any way?

7
  • Are you sure that you want 2, blue, medium in the results? Commented Jun 19, 2020 at 18:35
  • Where does (3, red, medium) come from? It's not in the original data Commented Jun 19, 2020 at 18:36
  • @a_horse_with_no_name you are absolutely right, so sorry. Just fixed Commented Jun 19, 2020 at 19:15
  • @forpas yes, I want it Commented Jun 19, 2020 at 19:16
  • There is no (3, red, big) either Commented Jun 19, 2020 at 19:16

1 Answer 1

1

With NOT EXISTS:

select t.* from tablename t where not exists ( select 1 from tablename where id <> t.id and color = t.color and size = t.size ); 

or with COUNT() window function:

select t.Id, t.color, t.size from ( select *, count(*) over (partition by color, size) counter from tablename ) t where t.counter = 1 

If you want to delete the duplicate rows from the table then:

delete from tablename t where exists ( select 1 from tablename where id <> t.id and color = t.color and size = t.size ); 

See the demo.
Results:

| id | color | size | | --- | ----- | ----- | | 3 | blue | small | | 4 | red | big | 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.