0
SELECT count(*) FROM bossy.talent
where talent.date >= '2019/01/31' and talent.date <= '2019/02/28'
and (country_id = 1 or country_id = 2) and (current_status = 'completed' or current_status = 'incompleted');

This query print 2637 which is the total number of completed and incompleted columns. But I need separate outputs for completed and incompleted like

2600
37

2600 is the number for completed and 37 is the number for incompleted.

1
  • An alternative to group by since you don't seem to want to print the status is to UNION ALL dev.mysql.com/doc/refman/8.0/en/union.html where the first query part is for completed and the second query part is for incompleted. Commented Jan 15, 2020 at 18:05

2 Answers 2

3

You can do this by combination of SUM function with CASE WHEN clause. For example, if we look at the completed, CASE WHEN will select 1 if current_status = 'completed' and 0 if current_status is anything else. And at the end when you sum that you get a final desired result.(1 + 1 + 0 = 2 for my example).

SELECT sum(case when current_status = 'completed' then 1
           else 0
           end) completed,
       sum(case when current_status = 'incompleted' then 1
           else 0
           end) incompleted
FROM  talent
where date between '2019/01/31' and '2019/02/28'
and country_id in (1, 2);

Here is a small DEMO

Here is how you can do it with UNION so you see one data under the other:

SELECT sum(case when current_status = 'completed' then 1
           else 0
           end) total_number
FROM  talent
where date between '2019/01/31' and  '2019/02/28'
and country_id in ( 1, 2) 
union
SELECT sum(case when current_status = 'incompleted' then 1
           else 0
           end) total_number
FROM  talent
where date between '2019/01/31' and  '2019/02/28'
and country_id in ( 1, 2) ;

Here is the DEMO for this second query.

Note that in this both query's you can use:

date between '2019/01/31' and '2019/02/28'

instead of :

talent.date >= '2019/01/31' and talent.date <= '2019/02/28'

and:

country_id in (1, 2)

instead of :

(country_id = 1 or country_id = 2) 
Sign up to request clarification or add additional context in comments.

9 Comments

Why would you do it like this?
OP's : "But I need separate outputs for completed and incompleted"
This produces 2 columns rather than the desired 2 rows.
@P.Salmon isn't that just an output/display issue?
@VBoka -- you didn't answer my question, I'm trying to get you to explain why your answer works, to make your answer better -- to show why this method is the correct one rather than using UNION or GROUP BY clauses. If you can edit your answer and add these details as to why, that would be a huge improvement. Thanks
|
2

Use GROUP BY :

SELECT current_status, count(*) 
FROM bossy.talent
WHERE talent.date >= '2019/01/31' AND 
      talent.date <= '2019/02/28' AND 
      country_id IN (1,2) AND
      current_status IN ('completed', 'incompleted')
GROUP BY current_status;

You can use IN instead of ORs. If you want further bifurcation by country then add it also in GROUP BY.

6 Comments

Why should GROUP BY be used here?
Thank you @Yogesh Sharma. What if i want to print total count along with completed and incompleted.
@AkkireddyChalla, read about GROUP BY ... WITH ROLLUP here: dev.mysql.com/doc/refman/8.0/en/group-by-modifiers.html
@Martin, GROUP BY current_status makes the query return one row for each distinct value of current_status. The COUNT(*) aggregation applies to each group of rows with the same value of current_status.
@BillKarwin ROLLUP is to add the outputs. But in this case I not only have completed and incompleted. My expected answer will be completed 2600, incompleted 37, total 7530. Total is the no of rows in the table.
|

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.