按天数进行统计的时候要注意

比如,统计客服 在 2018-08-21 与  2018-08-22 之间被投诉的次数,需要group by两次才可以

一次按照user_id去分组,一次按照时间去分组

date_format(q.oper_time,'%m-%d-%Y')
SELECT COUNT(s.user_id) AS compNum, user_id,date_format(q.oper_time,'%m-%d-%Y')
        FROM qc_complaint_schedule q
        LEFT JOIN sy_user_info s ON s.user_id = q.operator_id
      where
             progress_type = 1 and oper_time>="2018-08-21 16:00:40"  and oper_time < "2018-08-22 00:00:00"
        GROUP BY s.user_id, date_format(q.oper_time,'%m-%d-%Y')

分组后的效果为:会按照每个人每天被投诉的次数 去进行分组

如果没有加上按照时间分组的话,查询效果是这样:

id为2的人的两天的投诉记录就被合并为了一天 

原文地址:https://www.cnblogs.com/huanghuanghui/p/9520166.html