How do I select the rows that have either X or Y in column C but not both in Postgres? Basically there should be no duplicate values in Column B.
Data :
A | B | C |
----------------------------------------
1 | John | X |
----------------------------------------
2 | John | Y |
----------------------------------------
3 | Sam | X |
----------------------------------------
4 | May | Y |
Result:
A | B | C |
----------------------------------------
1 | John | X |
----------------------------------------
3 | Sam | X |
----------------------------------------
4 | May | Y |
2
Answers
You can use DISTINCT ON clause:
In that case you will get the first row within every unique B value.
You can also use WINDOW FUNCTIONS or GROUP BY clause if filtering with special conditions is required.
First, you can count occurencies of ‘X’ and ‘Y’ for the every name (this is what x_or_y does) and then just pick those with count = 1
enter link description here