how to get records from two tables where all for example statuses are true in the second table.
Table A Table B
id name id idA status
9 'name1' 20 9 true
21 9 false
7 'name2' 22 7 true
23 7 true
24 7 true
6 'name3' 30 6 false
31 6 true
32 6 false
output
result:
table A tableB
7 'name2' 22 7 true
23 7 true
24 7 true
I would like to get the results from table A where the data in table B are only true
3
Answers
You can use the aggregation function
bool_and
for this purpose. If it returns true for a givenidA
, the corresponding record intableA
must be returned.The magic is in the
HAVING bool_and(status)
which means "keep the idA for whichtrue
(implicit) is returned after aggregation".This can be solved in two ways:
Having "only true" means that there should not be any status with false in table b:
Alternatively one can use the aggregate function
bool_and()
to find those rows that have only true values for the status column:For your question I will use
NOT EXISTS
operator like this :it will not take any id that has
false
at least once