Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I count the number of zeros in sql?

I have a column that has a value that is either 0 or 1. how do i count the numbers of zeros?

SELECT A.id
FROM table1 A
GROUP BY A.id
HAVING SUM(A.zeroorone) >=
ALL (SELECT SUM(AA.zeroorone)
    FROM table1 AA
    GROUP BY AA.id)

I know this counts the number of ones. I cant seem to get it

like image 749
Robin Avatar asked Dec 06 '25 06:12

Robin


2 Answers

A CASE statement would do nicely:

SELECT SUM(CASE WHEN zeroorone = 0 
                THEN 1 
                ELSE 0 END) AS TOTAL_ZEROS
  FROM table1 
like image 51
mechanical_meat Avatar answered Dec 08 '25 21:12

mechanical_meat


Using count...

SELECT COUNT(*) FROM table1 
WHERE zeroone=0
like image 21
Icarus Avatar answered Dec 08 '25 21:12

Icarus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!