Introducing PopSQL + dbt. The first SQL editor with built-in dbt support.Read more →

How to Use Filter to Have Multiple Counts in PostgreSQL

Using filter is useful when you want to do multiple counts on a table:

  count(1), -- Count all users
  count(1) filter (where gender = 'male'), -- Count male users
  count(1) filter (where beta is true) -- Count beta users
  count(1) filter (where active is true and beta is false) -- Count active non-beta users
from users

Ready for a modern SQL editor?