题解 | #每份试卷每月作答数和截止当月的作答总数。#
每份试卷每月作答数和截止当月的作答总数。
https://www.nowcoder.com/practice/5f1cbe74c682485aa73e4c2b30f04a62
#每份卷子的每个月作答数量和每个卷子的start month
#每份卷子每个月的作答数量
#SELECT
# exam_id,
# date_format(start_time,'%Y%m') AS start_month,
# count(id) AS month_cnt
#FROM exam_record
#GROUP BY exam_id,date_format(start_time,'%Y%m')
#计算截至到当前的作答数量:
SELECT
exam_id,
start_month,
month_cnt,
SUM(month_cnt) OVER(PARTITION BY exam_id ORDER BY start_month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cum_exam_cnt
FROM (SELECT
exam_id,
date_format(start_time,'%Y%m') AS start_month,
count(id) AS month_cnt
FROM exam_record
GROUP BY exam_id,date_format(start_time,'%Y%m')
) AS t;