easyrider
easyrider

Reputation: 701

Query to get count of rows with and without a value - 1query

I want to count how many rows has given value (1) and count how many row has value(0)

id XXX 1 <==  value 0 or 1
id YYY 1
id ZZZ 0

so the result would be

ones | zeros
2    | 0

thx in advance

Upvotes: 0

Views: 49

Answers (2)

Dan Bracuk
Dan Bracuk

Reputation: 20804

Something like this should work:

select sum(case when value = 1 then 1 else 0 end) ones
, sum(case when value = 0 then 1 else 0 end) zeros

Upvotes: 2

ffflabs
ffflabs

Reputation: 17481

Try this

select sum(if(value=0,1,0)) as zeros,
       sum(if(value=1,1,0)) as ones
from mytable

Upvotes: 1

Related Questions