I have a table called 'apple' and I wrote the following query:
select name,
count(name),
case when istasty is null then false else istasty end
from apple
group by name, istasty;
This the output:
I am trying to group the name
and istasty
flag using following condition:
- When the corresponding name column has both true or false, then I return false. In the above image,
tala
has bothtrue
andfalse
istasty
column. However, I want to return false because it has atleast one falseistasty
column. - If after grouping all
istasty
column for a particular name column istrue
then returntrue
; Similarly, if allistasty
column isfalse
, then returnfalse
for that particular name column.
I can achieve using the bool_and
operator in postgresql
by writing the query:
select name, count(name), bool_and(coalesce(istasty = true, false)) from apple group by name;
Is there any way I can modify my first query so that I add filter in having clause
to achieve the same result as I got using bool_and
operator in second query? Or is there any other possible way?
Please note I am not using bool_and
operator. I appreciate your time. Thank you.