I have a query now that shows me all the calls between this time, is there a way to format it to display “day (9-5)” and “night (5-12)” in one query?
select dst, count(dst) as Day from cdr
WHERE lastapp='Queue'
AND Date(calldate) = '2012-03-23'
AND TIME(calldate) BETWEEN '08:00' AND '17:00'
GROUP BY dst;
+------+-----+
| dst | Day |
+------+-----+
| 1010 | 10 |
| 1011 | 21 |
| 1012 | 7 |
+------+-----+
What I would ideally want is this so I can grab it in an array in PHP without having to do 2 queries.
+------+-----+-------+
| dst | Day | Night |
+------+-----+-------+
| 1010 | 10 | 20 |
| 1011 | 21 | 12 |
| 1012 | 7 | 4 |
+------+-----+-------+
Yes; you can write something like this:
(
COUNT(...)counts the number of rows for which...was non-null.IF(boolean, value_if_true, value_if_false)does what it sounds like — see http://dev.mysql.com/doc/refman/5.6/en/control-flow-functions.html#function_if — though I should mention that it’s a MySQL-specific function. If you hadn’t specifically tagged your question as MySQL, I would have suggestedCASE WHEN boolean THEN value_if_true ELSE value_if_false ENDinstead, since that’s a standard syntax that most or all DMBSes support.)