业务场景
对某类业务数据,按月统计数量,返回当年各个月份的任务数量。
思路
按照创建时间,格式化为yyyy-mm的month字段,然后根据month进行分组查询,统计count作为数量
SELECT DATE_FORMAT(create_time, '%Y-%m') AS month,COUNT(*) as allNum,COUNT(IF(status = 4 and DATE_FORMAT(create_time, '%Y-%m') = DATE_FORMAT(end_time, '%Y-%m'), TRUE,NULL)) as completeNumFROM engineering_taskWHERE task_type = 5AND create_time between '2023-01-01' AND '2023-09-19'AND is_del = 0GROUP BY month
结果示例