题解 | #月总刷题数和日均刷题数#

月总刷题数和日均刷题数

http://www.nowcoder.com/practice/f6b4770f453d4163acc419e3d19e6746

(SELECT DATE_FORMAT(submit_time,'%Y%m') AS submit_month,
COUNT(*) AS month_q_cnt, 
ROUND(COUNT(*)/DAY(LAST_DAY(submit_time)),3) AS avg_day_q_cnt
FROM practice_record
WHERE YEAR(submit_time)=2021
GROUP BY submit_month

UNION

SELECT '2021汇总' AS submit_month,
COUNT(*) AS month_q_cnt,
ROUND(COUNT(*)/31,3) AS avg_day_q_cnt
FROM practice_record
WHERE YEAR(submit_time)=2021)
ORDER BY submit_month

这里要mark的是: (1)获取对应月份的天数:LAST_DAY(date) 输入一个日期或日期时间值,返回该月的最后一天对应的日期。 (2)汇总行的构建:通过UNION添加汇总行,汇总行的名字利用【SELECT '2021汇总' AS submit_month】解决。

全部评论

相关推荐

object3:开始给部分🌸孝子上人生第一课了
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务