I have following table in PostgreSQL 11.0
col1 col2 col3 col4
1 a a a
1 a a a_1
1 a a a_2
1 b b c
2 d d c
3 e d e
I would like to filter above table such that if col2 and col4 are equal, only this match should be selected and below two rows are excluded. When col2 and col4 are not equal, rows with col2 = col3 should be kept.
The desired output is:
col1 col2 col3 col4
1 a a a
1 b b c
2 d d c
3 e d e
I am trying following query with no success so far.
select * from table1
where col2=col4
union
select * from table1
where col2 != col4 and col2=col3
but this will include rows where there is already a match, which I want to exclude in the final output.
1 a a a_1
1 a a a_2