I need fast and effective method to get three count from one table for statistic on my current personal project. And then I’ve found easy and elegant way to do so:

select count(*) Total,  
sum(case when is_approved = 't' then 1 else 0 end) PendingCount,
sum(case when is_rejected = 't' then 1 else 0 end) RejectedCount
from project_project

You could add group by statement so this code will show you counts for groups. This code I’ve tested in PostgresSQL. I’ll try to test it with Oracle DB and will write an update.

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store