0

In my table I have task, every task have state.

1 - planned
2 - executing
3 - finished

I want get count of all planned, executing and finished task.

I can write three queries like this:

SELECT COUNT(*) FROM `task` WHERE state = 1
SELECT COUNT(*) FROM `task` WHERE state = 2
SELECT COUNT(*) FROM `task` WHERE state = 3

So, my question is: It is possible (and how?) get this data in one query?

Thank you for any help.

1
  • You should tag your question with the database you are using Commented Jan 25, 2018 at 22:20

2 Answers 2

6

another approach is to use group by, like this:

select state, count(*)
from task
group by state
Sign up to request clarification or add additional context in comments.

1 Comment

of course.. funny how we can easily forget other methods aye!
5

you use SUM

select state,
sum(case when state = 1 then 1 else 0 end) state1count,
sum(case when state = 2 then 1 else 0 end) state2count,
sum(case when state = 3 then 1 else 0 end) state3count
from task

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.