select distinct exam_id, date_format(start_time, '%Y%m') as start_month, count(start_time) over (partition by exam_id, date_format(start_time, '%Y%m')) as month_cnt, count(start_time) over (partition by exam_id order by date_format(start_time, '%Y%m')) as cum_exam_cnt from exam_record order by exam_id, start_month
1. 先按exam_id分组,再按年月分组,并统计个数。即为每份试卷每个月的作答数。
2.先按exam_id分组,再按年月排正序,并统计个数。即为每份试卷截至当月的作答总数。