Skip to content Skip to sidebar Skip to footer

Postgresql - Select Distinct(column1, Column2) Where A Condition Holds

I have the following table and some sample records in it: id | attr1_id | attr2_id | user_id | rating_id | ------+----------+----------+-------------------+-----------

Solution 1:

I think this does what you describe:

select least(attr1_id, attr2_id) as attr1, greatest(attr1_id, attr2_id) as attr2
from table t
group by least(attr1_id, attr2_id), greatest(attr1_id, attr2_id) 
having bool_and(rating_d = 1) ;

I don't understand the other tables in your query, because your start with a single table that has everything you need.

Post a Comment for "Postgresql - Select Distinct(column1, Column2) Where A Condition Holds"