S
S
Schrödinger's cat2018-05-29 07:36:38
PostgreSQL
Schrödinger's cat, 2018-05-29 07:36:38

How to check multiple conditions in having when using array_agg?

the sets table also has a group column, which can be 1 or 2.
How to rewrite the query to take into account not only the fact that the found rows contain the ARRAY[1,2,3] array, but also that all of them belong to only one of the groups (1 or 2)

SELECT country_id FROM countries 
WHERE EXISTS 
  (SELECT 1 FROM sets 
  WHERE (sets.country_id = countries.country_id AND sets.is_used = true) 
  HAVING(array_agg(sets.city_id) @> ARRAY[1, 2, 3]))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
ponaehal, 2018-06-07
@ponaehal

As far as I remember, the youth of HAVING filters by aggregator values ​​(SUM, MAX, etc.), and in your case, you just need to add a filter to the WHERE .... AND group in (1,2) condition

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question