1

the table:

tab_tasks

id|....|date_completed|completed
1 |    |  2016-11-05  |   Y
2 |    |  2016-11-07  |   N
3 |    |  2016-11-09  |   Y
4 |    |  2016-12-11  |   Y
5 |    |  2017-01-15  |   Y
6 |    |  2017-01-30  |   Y

and so on...

I need help in writing sql query by giving current date and get months with counted completed tasks back to max 12 months something like:

month        | num_complited
november     |     2
december     |     1
january      |     2

or if it would be easier the instead of letters of the alphabet, month can be numeric 11, 12, 01 in column month.

How can be this accomplished? Is there a need for any php code or can be this done with just sql query? Or should I add extra column to that table, like month?

2 Answers 2

1

you should put some date range otherwise for example 11-2016 has 6 count and 11-2017 has 5 so out put will show 11 count data mix-up

 SELECT MONTHNAME(date_completed) as month ,
  count(completed)  as num_complited 
  from tab_tasks 
  where completed='Y' and 
  date_completed between '2016-11-05'  and '2017-11-05' 
  group by MONTHNAME(date_completed)
Sign up to request clarification or add additional context in comments.

3 Comments

this answer seems to be what I'd need, but I don't know why I get empty result if I add your date range? (of course my records are in that date range) When I remove the date condition then shows fine, any idea?
may be you try like '2016-11-05'
can you try now I have added single quotas
0

Try this:

select MONTHNAME(date_completed) as MosName,count(1) as num_completed  
from TableName
where completed = 'Y'
GROUP BY MONTHNAME(date_completed);

1 Comment

you forget to add from 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.