MySQL统计过去12个月的数据。

288 阅读1分钟

当前月份往前推12个月份的数据信息。

select DATE_FORMAT(cwo.create_time, '%Y-%m') as createDate, 
count(case when (cwo.process_method_code = 1 or cwo.process_method_code = 2) then 1 END) as hdCount,
count(case when cwo.process_method_code = 3 then 1 END) as siteCount
from ccs_work_order cwo
group by createDate 
order by createDate desc 
limit 12

结果展示: 在这里插入图片描述 因为我的数据中create_time字段出现了NULL,所以出现了一条NULL记录。大家可以过滤掉。