How can I use SUM for bit columns?
问题 How can use the function SUM() for bit columns in T-SQL? When I try do it as below: SELECT SUM(bitColumn) FROM MyTable; I get the error: Operand data type bit is invalid for sum operator. 回答1: SELECT SUM(CAST(bitColumn AS INT)) FROM dbo.MyTable need to cast into number or another solution - SELECT COUNT(*) FROM dbo.MyTable WHERE bitColumn = 1 回答2: You could consider 0 as nulls and simply count the remaining values: SELECT count(nullif(bitColumn, 0)) FROM MyTable; 回答3: You can achieve by using