Skip to content Skip to sidebar Skip to footer

How To Count By Filter In Sql Query?

This is a part of my SQL query: COUNT(t1.id) AS all_statuses_count, COUNT(t1.status = 'completed') AS completed_statuses_count What I want to get is count objects with ALL statuse

Solution 1:

SELECT COUNT(t1.id) AS all_records,
       SUM(CASE WHEN t1.status = 'completed' THEN 1 ELSE 0 END) AS completed_status_count
FROM t1

Something like this should work. A bit confused about your first count though. Maybe you're looking for something a bit more window-function based. I honestly can't tell.


Post a Comment for "How To Count By Filter In Sql Query?"