Skip to content
Advertisement

Intersection of Records in Postgres

Suppose I have labels with multiple stores associated with them like so:

Is there any good way in SQL (or jooq) to get all the store ids in the intersection of the labels? Meaning just return store_2 in the example above because store_2 is associated with label_1, label_2, and label_3? I would like a general method to handle the case where I have n labels.

Advertisement

Answer

Then convert the query by @GMB into an SQL function that takes an array and returns a table of store_id’s.

Then all that’s needed is a simple select. See complete example here.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement