MySQL Crosstab查询按月返回默认值1?

时间:2018-08-01 13:51:13

标签: mysql

如果该月没有值,为什么我告诉我查询默认为“ 0”,为什么查询返回“ 1”?

这是我的结果: enter image description here

SELECT  td_type,

COUNT(DISTINCT CASE WHEN DATE_FORMAT(ttDate,'%Y-%m') = DATE_FORMAT(NOW(), '%Y-%01') THEN (ttNum)    ELSE    0   END ) AS 'Jan',
COUNT(DISTINCT CASE WHEN DATE_FORMAT(ttDate,'%Y-%m') = DATE_FORMAT(NOW(), '%Y-%02') THEN (ttNum)    ELSE    0   END ) AS 'Feb',
COUNT(DISTINCT CASE WHEN DATE_FORMAT(ttDate,'%Y-%m') = DATE_FORMAT(NOW(), '%Y-%03') THEN (ttNum)    ELSE    0   END ) AS 'Mar',
COUNT(DISTINCT CASE WHEN DATE_FORMAT(ttDate,'%Y-%m') = DATE_FORMAT(NOW(), '%Y-%04') THEN (ttNum)    ELSE    0   END ) AS 'Apr',
COUNT(DISTINCT CASE WHEN DATE_FORMAT(ttDate,'%Y-%m') = DATE_FORMAT(NOW(), '%Y-%05') THEN (ttNum)    ELSE    0   END ) AS 'May',
COUNT(DISTINCT CASE WHEN DATE_FORMAT(ttDate,'%Y-%m') = DATE_FORMAT(NOW(), '%Y-%06') THEN (ttNum)    ELSE    0   END ) AS 'Jun',
COUNT(DISTINCT CASE WHEN DATE_FORMAT(ttDate,'%Y-%m') = DATE_FORMAT(NOW(), '%Y-%07') THEN (ttNum)    ELSE    0   END ) AS 'Jul',
COUNT(DISTINCT CASE WHEN DATE_FORMAT(ttDate,'%Y-%m') = DATE_FORMAT(NOW(), '%Y-%08') THEN (ttNum)    ELSE    0   END ) AS 'Aug',
COUNT(DISTINCT CASE WHEN DATE_FORMAT(ttDate,'%Y-%m') = DATE_FORMAT(NOW(), '%Y-%09') THEN (ttNum)    ELSE    0   END ) AS 'Sep',
COUNT(DISTINCT CASE WHEN DATE_FORMAT(ttDate,'%Y-%m') = DATE_FORMAT(NOW(), '%Y-%10') THEN (ttNum)    ELSE    0   END ) AS 'Oct',
COUNT(DISTINCT CASE WHEN DATE_FORMAT(ttDate,'%Y-%m') = DATE_FORMAT(NOW(), '%Y-%11') THEN (ttNum)    ELSE    0   END ) AS 'Nov',
COUNT(DISTINCT CASE WHEN DATE_FORMAT(ttDate,'%Y-%m') = DATE_FORMAT(NOW(), '%Y-%12') THEN (ttNum)    ELSE    0   END ) AS 'Dec',
            COUNT(ttNum) AS Total

FROM      v_closed_tickets
WHERE (DATE_FORMAT(ttDate, '%Y%') = DATE_FORMAT(NOW(), '%Y%')) AND ttCategory IN ('TT') 

GROUP BY ccOption

0 个答案:

没有答案