2

PostreSQL features the every() aggregate functions, which filters groups by running a predicate on all group rows.

For example, for the following table:

 star_name | star_type | landed_upon
-----------+-----------+-------------
 Mars      | Planet    | t
 Venus     | Planet    | t
 Rhea      | Moons     | f
 Titan     | Moons     | t

This every() query:

SELECT star_type, max(star_name) example, COUNT(*)
    FROM stars
    GROUP BY star_type
    HAVING every (landed_upon = true);

Returns the Planet group but not the Moons group, because Rhea does not satisfy the landed_upon predicate within the Moons group:

 star_type | example | count
-----------+---------+-------
 Planet    | Venus   |     2
(1 row)

Is there an equivalent SQLAlchemy operator for PostgreSQL every()?

2
  • I can't test it at the moment but .having(func.every(Star.landed_upon)) should work. Commented Jun 10, 2015 at 13:03
  • 1
    @Marth Excellent, it works. Care to make it an answer so I can accept it? Commented Jun 10, 2015 at 16:00

1 Answer 1

1

You can use the func object to generate SQL functions.

.having(func.every(Star.landed_upon))

will generate

HAVING EVERY(stars.landed_upon)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.