How to get the true/false count from a bit field into two separate columns

avgbody picture avgbody · Oct 7, 2009 · Viewed 37k times · Source

I need to create a query that will sum the number of True(1) and False(0) into two separate columns from one bit field.

I'm joining 3 tables and need it to be something like:

Attribute | Class | Pass | Fail

I will be grouping on Attribute and Class.

Answer

Matthew Jones picture Matthew Jones · Oct 7, 2009

Something like this:

SUM(CASE WHEN ColumnName = 1 THEN 1 ELSE 0 END) AS Pass, 
SUM(CASE WHEN ColumnName = 0 THEN 1 ELSE 0 END) AS Fail