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?
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.
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.
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 .
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.
Another method to COUNT
only 1 from BIT
column is to use NULLIF:
SELECT COUNT(NULLIF(TagBased,0)) AS result
FROM Badges b;
LiveDemo
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With