Select Count of Certain Rows as Column

I have a table with a "status" column which accepts a TINYINT. For example:

name   | status
-------+--------
john   | 0
joe    | 1
johann | 0
jan    | 1
jane   | 0

How can I get a count of who is status 1 and who is status 0?

status1 | status0
--------+--------
2       | 3

Answers


Just use conditional aggregation:

select sum(status = 1) as status1, sum(status = 0) as status0
from t;

In your case, you could also write this as:

select sum(status) as status1, sum(1 - status) as status0
from t;

I would use a CASE statement to check for your value.

SELECT SUM(CASE WHEN [status] = 1 THEN 1 ELSE 0 END) AS Status1,
    SUM(CASE WHEN [status] = 0 THEN 1 ELSE 0 END) AS Status0
FROM tbl;

Need Your Help

How to apply video filtering in android?

android video android-ndk ffmpeg

I am working on app like instagram where i have to apply filters on already created video and stored it in SDCard. I have searched a lot but at the end of day i find one library named FFMPEG but di...

Distributed WSO2 CEP

wso2 distributed complex-event-processing wso2cep

If I have a distributed CEP setup with a JMS broker as the primary input.