【牛客】SQL127 月总刷题数和日均刷题数

描述

现有一张题目练习记录表practice_record,示例内容如下:

|----|------|-------------|---------------------|-------|
| id | uid | question_id | submit_time | score |
| 1 | 1001 | 8001 | 2021-08-02 11:41:01 | 60 |
| 2 | 1002 | 8001 | 2021-09-02 19:30:01 | 50 |
| 3 | 1002 | 8001 | 2021-09-02 19:20:01 | 70 |
| 4 | 1002 | 8002 | 2021-09-02 19:38:01 | 70 |
| 5 | 1003 | 8002 | 2021-08-01 19:38:01 | 80 |

请从中统计出2021年每个月里用户的月总刷题数month_q_cnt 和日均刷题数avg_day_q_cnt(按月份升序排序)以及该年的总体情况,示例数据输出如下:

|--------------|-------------|---------------|
| submit_month | month_q_cnt | avg_day_q_cnt |
| 202108 | 2 | 0.065 |
| 202109 | 3 | 0.100 |
| 2021汇总 | 5 | 0.161 |

解释:2021年8月共有2次刷题记录,日均刷题数为2/31=0.065(保留3位小数);2021年9月共有3次刷题记录,日均刷题数为3/30=0.100;2021年共有5次刷题记录(年度汇总平均无实际意义,这里我们按照31天来算5/31=0.161)

sql 复制代码
# 获取当月的天数:dayofmonth(last_day(submit_time))

select
date_format (submit_time,'%Y%m') as submit_month,
count(1) as month_q_cnt,
round(count(1)/ max(dayofmonth(last_day(submit_time))),3) as avg_day_q_cnt
from practice_record
where left(submit_time,4)=2021
group by submit_month

union all

select
'2021汇总' as submit_month,
count(1) as month_q_cnt,
round(count(1)/ 31,3)as avg_day_q_cnt
from practice_record
where left(submit_time,4)=2021

order by submit_month
相关推荐
jiayou6418 小时前
KingbaseES 实战:深度解析数据库对象访问权限管理
数据库
于眠牧北19 小时前
MySQL的锁类型,表锁,行锁,MVCC中所使用的临键锁
mysql
李广坤2 天前
MySQL 大表字段变更实践(改名 + 改类型 + 改长度)
数据库
Turnip12023 天前
深度解析:为什么简单的数据库"写操作"会在 MySQL 中卡住?
后端·mysql
爱可生开源社区3 天前
2026 年,优秀的 DBA 需要具备哪些素质?
数据库·人工智能·dba
随逸1773 天前
《从零搭建NestJS项目》
数据库·typescript
加号33 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
シ風箏3 天前
MySQL【部署 04】Docker部署 MySQL8.0.32 版本(网盘镜像及启动命令分享)
数据库·mysql·docker
李慕婉学姐3 天前
Springboot智慧社区系统设计与开发6n99s526(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
百锦再3 天前
Django实现接口token检测的实现方案
数据库·python·django·sqlite·flask·fastapi·pip