Skip to content
Advertisement

Filtering in count function – Postgresql

I have the following query which gives me the amount of referrals users have. However, I would like to only count the referral if the referred user has activated the premium plan. How could this be achieved?

Dbfiddle here.

I am using PostgreSQL version 14.

Advertisement

Answer

You can try to use FILTER clause or condition aggregate function.

Edit

From you comment you I think you can try to use subquery to do aggregate function to find premium user first, then do OUTER JOIN for user table.

sqlfiddle

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