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

Viewed 42773

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.

6 Answers

This works (at least in SQL 2008)

SELECT SUM(Passed + 0) PASS , SUM(1 - Passed) FAIL

I am adding 0 to Passed in the first sum as a short hand way of converting from bit to int since you can't sum bits directly.

Related