KeiruaProd

I help my clients acquire new users and make more money with their web businesses. I have ten years of experience with SaaS projects. If that’s something you need help with, we should get in touch!
< Back to article list

PostgreSQL snippet: multiple counts in one query

Occasionnaly you’ll want to have multiple counts with only one SQL query. That’s where the filter keyword comes into play:

select
    count(*) as "Total subscriptions",
    count(*) filter (where state = 'new') as "New subscriptions",
    count(*) filter (where state = 'pending') as "Pending subscriptions"
from user_subscriptions;

Here is postgres’ doc on the topic.