0

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

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

Comments

1

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.