题解 | #月总刷题数和日均刷题数#
月总刷题数和日均刷题数
https://www.nowcoder.com/practice/f6b4770f453d4163acc419e3d19e6746
SELECT DATE_FORMAT(submit_time,"%Y%m") submit_month, COUNT(submit_time) month_q_cnt, round(COUNT(submit_time) / MAX(DAY(last_day(submit_time))),3)avg_day_q_cnt FROM practice_record WHERE year(submit_time) = '2021' GROUP BY DATE_FORMAT(submit_time,"%Y%m") union all SELECT '2021汇总' submit_month,count(submit_time) month_q_cnt,round(count(submit_time) /31 ,3)avg_day_q_cnt FROM practice_record WHERE year(submit_time) = '2021' ORDER BY submit_month
union all 在最后一行结果拼接,列名要相同
简洁函数报错:
函数:
with rollup :将分组后的结果进行加和
coalesce(expression,1) 如果表达式为null返回1