题解 | #月总刷题数和日均刷题数#
月总刷题数和日均刷题数
https://www.nowcoder.com/practice/f6b4770f453d4163acc419e3d19e6746
本题要点有如下几个:
1、每月天数的计算,查阅资料发现可以先使用last_day()函数得出每月的最后一天,再使用day()函数获取最后一天的数值;此外,使用day函数获取天数后,每条数据都有这个参数,因此需要使用一个聚合函数或是其它函数将其去重;
2、把月份数据同年度数据放在一个表上,这点在参考题解前我并没有很好的思路,同样是在查阅资料后发现可以使用union all函数;
其它的点此前的题目都有遇到过。
select date_format(submit_time, "%Y%m") submit_month, count(question_id) month_q_cnt, round( count(question_id) / avg(day(last_day(submit_time))), 3 ) avg_day_q_cnt from practice_record where year(submit_time) = 2021 and score is not null group by date_format(submit_time, "%Y%m") union all select '2021汇总' submit_month, count(question_id) month_q_cnt, round(count(question_id) / 31, 3) avg_day_q_cnt from practice_record where year(submit_time) = 2021 and score is not null order by submit_month asc