postgresql 9.6, windows 7
Here is an example of a database :
fld1 fld2
abc D123
xyz D123
abc D789
how can I select rows with fld1='abc'
if/only for fld2 value exists just one time in the database ?
In my example, I only want to select the third row.
Best Answer
I have extended a bit your sample data:
You can use EXIST in your WHERE clause with a dataset grouped by
fld2
:db<>fiddle here
Or you can use a CTE to get all
fld2
rows with a count of 1, and join it with all rows wherefld1 = 'abc'
:db<>fiddle here