Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a count of a bit-type column?

I'm trying to use SEDE to determine how many tag-based badges have been awarded in total. My typical strategy is to sum:

select sum(TagBased)
from Badges

Unfortunately, since TagBased is a bit value, I get this error:

Operand data type bit is invalid for sum operator.

Out of desperation, I tried count:

select count(TagBased)
from Badges

This counts the number of non-null values, which in this case is identical to count(*). So how can a bit value be used in aggregate?

like image 592
Jon Ericson Avatar asked Aug 27 '15 16:08

Jon Ericson


People also ask

How do I get a COUNT of a specific column?

In SQL, you can make a database query and use the COUNT function to get the number of rows for a particular group in the table. Here is the basic syntax: SELECT COUNT(column_name) FROM table_name; COUNT(column_name) will not include NULL values as part of the count.

How do I sum a bit column in SQL?

SELECT SUM(CASE WHEN bitColumn = 1 THEN 1 ELSE 0 END) FROM MyTable; Or, if you're really keen on code compression IIF . Save this answer.

How do I get the column COUNT in a table in SQL?

In the Microsoft SQL server, the DESC command is not an SQL command, it is used in Oracle. SELECT count(*) as No_of_Column FROM information_schema. columns WHERE table_name ='geeksforgeeks'; Here, COUNT(*) counts the number of columns returned by the INFORMATION_SCHEMA .


2 Answers

If you just want to look at tag-based badges and don't care about the rest, you can move the test to the where clause:

select count(*)
from Badges 
where TagBased = 1

The canonical solution would be to cast the bit value to a data type that can be summed:

select sum(cast(TagBased as decimal))
from Badges

An even more flexible solution is to do the conversion yourself with a CASE statement:

select sum(case TagBased when 1 then 100.0 end)/count(*) tag_based_per
from Badges

Alternatively, in newer versions of SQL Server there's an IIF function:

select sum(iif(TagBased = 1, 1, 0))
from Badges

Finally, if you are golfing, you can trick SQL into converting the bit for you:

select sum(TagBased+0)
from Badges

The credit for this technique goes to Kenneth Baltrinic's answer to a more specific question.

For reference (and potential forking): my test queries.

like image 94
Jon Ericson Avatar answered Oct 20 '22 12:10

Jon Ericson


Another method to COUNT only 1 from BIT column is to use NULLIF:

SELECT COUNT(NULLIF(TagBased,0)) AS result
FROM Badges b;

LiveDemo


One more method using [SIGN](https://msdn.microsoft.com/en-us/library/ms188420.aspx):
SELECT SUM(SIGN(TagBased)) AS result
FROM Badges b;

LiveDemo2


And one more way. I am not sure why I've written it:

SELECT DISTINCT TOP 1 RANK() OVER(ORDER BY TagBased DESC) - 1
FROM Badges b
ORDER BY 1 DESC;

LiveDemo3

like image 33
Lukasz Szozda Avatar answered Oct 20 '22 12:10

Lukasz Szozda