select date_format(submit_time, "%Y%m") as submit_month, count(submit_time) as month_q_cnt, round(count(submit_time) / day(last_day(submit_time)),3) as avg_day_q_cnt from practice_record where date_format(submit_time, '%Y') = '2021' group by date_format(submit_time, "%Y%m") union all select '2021汇总' as submit_month, count(submit_time) as month_q_cnt, round(count(submit_time)/31, 3) as avg_day_q_cnt from practice_record where date_format(submit_time, '%Y') = '2021' order by submit_month
1.用date_format(submit_time, '%Y') = '2021'筛选出2021年的记录
2.用group by submit_month按月份分组,但首先要从submit_time字段值中取得月份值,
即用date_format(submit_time, "%Y%m") as submit_month
3.分组之后用count对submit_time进行计数,作为月刷题数mon_q_cnt
4.用day(last_day(submit_time))确定当月的天数
5.用round(月刷题总数 / 当月总天数,3)即为日刷题数,保留3位小数
6.除了月份记录还有汇总记录,用union all
7, 用count对submit_time进行计数,因为没有按月份分组,所以总数是年度刷题总数
8.rount(年刷题总数 / 31 , 3)即为年平均刷题量
9.order by submit_month按月份升序排列