Count multiple columns grouped by date My current query looks like this

SELECT `task`, COUNT(*) as sum 
FROM tasks 
where `datetask` between ? and ? 
  AND `taskstatus` = ?  
GROUP BY DATE(`datetask`) 
ORDER BY `datetask` ASC


Parameter status is currently 1. What If I wanted to make multiple status count up? Like 3-4 per date

"query": [
      {
        "datetask": "2015-11-04 00:00:00",
        "status1": "1"
      },
      {
        "datetask": "2015-11-05 00:00:00",
        "status1": "1"
      }
    ]


What I want to accomplish

DATE         | Status 1  | Status 2 | Status 3 
---------------------------------------------
2015-11-04   | 3         | 3        | 3
2015-11-05   | 2         | 3        |
2015-11-06   | 5         | 3        | 3
First, some comment on the existing query: 


Every expression that is not in the GROUP BY list should not appear in the SELECT list, unless it is inside an aggregate function (SUM(), COUNT(), etc). So that SELECT task should probably be changed to SELECT DATE(datetask) AS date_of_task.
It's not good to use BETWEEN .. AND .. for ranges of datetime and timestamp columns. The condition WHERE datetask BETWEEN '2015-10-01' AND '2015-10-31' will seemingly gather all rows of this October. Except that almost all the rows of the 31st of October will escape!  

The reason is that the condition is equivalent to WHERE datetask BETWEEN '2015-10-01 00:00:00' AND '2015-10-31 00:00:00' and all rows with datetime values that are not in midnight exactly (00:00:00) will fail the condition and will not be counted in the query.  

Even if you use something like WHERE datetask BETWEEN '2015-10-01 00:00:00' AND '2015-10-31 23:59:59', you have a few drawbacks: It's more complicated to write. It will work but only if the precision of the dtateime/timestamp is 1 second. If the column has millisecond or microsecond precision (this is allowed in 5.6+ versions), you'll have to change the condition to have ... AND '2015-10-31 23:59:59.999999'.    

My suggestion is to use the WHERE column &gt;=? AND column &lt; (? + INTERVAL 1 DAY) pattern. It's guaranteed to work on all DATE, DATETIME and TIMESTAMP types of arbitrary precision.


Now for the multiple counts, there are several methods. Perhaps the easiest to write is:

SELECT DATE(datetask) AS date_of_task,
       COUNT(CASE WHEN status = 1 THEN 1 END) AS status_1,
       COUNT(CASE WHEN status = 2 THEN 1 END) AS status_2,
       COUNT(CASE WHEN status = 3 THEN 1 END) AS status_3
FROM tasks 
WHERE datetask &gt;= ? AND datetask &lt; (? + INTERVAL 1 DAY) 
  AND taskstatus = ?  
GROUP BY DATE(datetask) 
ORDER BY date_of_task ASC ;


which  can be simplified a bit, to:

SELECT DATE(datetask) AS date_of_task,
       SUM(status = 1) AS status_1,
       SUM(status = 2) AS status_2,
       SUM(status = 3) AS status_3
FROM tasks 
WHERE datetask &gt;= ? AND datetask &lt; (? + INTERVAL 1 DAY) 
  AND taskstatus = ?  
GROUP BY DATE(datetask) ASC ;    -- combine GROUP BY and ORDER BY