Skip to content
Advertisement

Select distinct columns for which all rows meet a condition

I want to select the distinct a, b‘s for which all the c‘s have a specific value (in my case null)

So given the rows below

a   b   c
AN8 USD 99
AN8 USD 
AT0 EUR 
AT0 EUR 

the result should be

a   b
AT0 EUR

I have failed to successfully translate that to a query, could you please help me out?

Advertisement

Answer

Try:

select a, b from foo group by a, b having bool_and(c is null);

where bool_and is a postgres aggregate function.

Or maybe this, should work with ANSI sql I think:

select a, b from foo group by a, b having count(c) = 0;
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement