I have a table with two columns like this:
Column A | Column B |
---|---|
1 | 1 |
1 | 2 |
1 | 3 |
2 | 1 |
2 | 2 |
3 | 1 |
3 | 3 |
How do I query in postgres "Give me the distinct values of A such that have B contains both [1, 2] when A is a constant?" The expected result of such a query is A = [1, 2] from the simple table above.
I have been reading the postgres documentation and have written some queries, but I feel like I am missing something obvious here.
This is coming from a ManyToMany relation’s join table and I am also thinking about revisiting the relation there — perhaps I am looking at this in a more complicated way than it needs to be. But I have use cases for both query sides, e.g.:
"Given a set of B, give me all instances of A that contain all given values of B"
as well as
"Given a set of A, give me all the instances of B that are related to A"
2
Answers
This is a common query, and one canonical approach uses aggregation:
For the exact case of checking for only 2 distinct B values, we could also have written the
HAVING
clause as:and this might be sargable (i.e. can use an index on the
B
column).COUNT(DISTINCT CASE WHEN "Column B" IN (1, 2) THEN "Column B" END) counts the distinct values of "Column B" that are either 1 or 2.
The = 2 condition ensures that the group has both values 1 and 2 in "Column B".
This query will return the distinct values of "Column A" for which all specified values of B (1 and 2) are present.