How can I use SUM for bit columns?

前端 未结 7 1070
栀梦
栀梦 2021-01-01 10:45

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 e

7条回答
  •  别那么骄傲
    2021-01-01 11:21

    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
    

提交回复
热议问题