当前位置: 首页 > article >正文

牛客网SQL进阶127: 月总刷题数和日均刷题数

  官网链接:

月总刷题数和日均刷题数_牛客题霸_牛客网现有一张题目练习记录表practice_record,示例内容如下:。题目来自【牛客题霸】icon-default.png?t=N7T8https://www.nowcoder.com/practice/f6b4770f453d4163acc419e3d19e6746?tpId=240

0 问题描述

    基于练习记录表practice_record,统计出2021年每个月里用户的月总刷题数month_q_cnt 和日均刷题数avg_day_q_cnt(按月份升序排序)以及该年的总体情况,示例数据输出如下:

1 数据准备

CREATE TABLE  practice_record (
    id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
    uid int NOT NULL COMMENT '用户ID',
    question_id int NOT NULL COMMENT '题目ID',
    submit_time datetime COMMENT '提交时间',
    score tinyint COMMENT '得分'
)CHARACTER SET utf8 COLLATE utf8_general_ci;

INSERT INTO practice_record(uid,question_id,submit_time,score) VALUES
(1001, 8001, '2021-08-02 11:41:01', 60),
(1002, 8001, '2021-09-02 19:30:01', 50),
(1002, 8001, '2021-09-02 19:20:01', 70),
(1002, 8002, '2021-09-02 19:38:01', 70),
(1003, 8002, '2021-08-01 19:38:01', 80);

2 数据分析

方式一:union all 常规做法

-- 方式一:
select
    DATE_FORMAT(submit_time,'%Y%m') as submit_month,
    count(1)as month_q_cnt,
    round(count(1)/ max(day(last_day(submit_time))) ,3) as avg_day_q_cnt
from practice_record
where year(submit_time) = '2021'
 group by DATE_FORMAT(submit_time,'%Y%m')
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 score is not null and year(submit_time) = '2021'
order by submit_month;

上述代码用到的函数:last_day()返回参数日期的最后一天,day(last_day())返回当月的天数

ps:这里最容易出错的点在于:每月天数的计算

(1) 计算每个月的天数可以用函数:day(last_day(datetime));
(2) 一年12月,每个月的天数: case when month(datetime) in (1,3,5,7,8,10,12) then 31 else 30 end 
(3) 最容易出错的点在于 : group by DATE_FORMAT(submit_time,'%Y%m') 分组后,select后面只能跟:group by 分组字段、常量、以及 count()/ max()/min()/avg()/sum()等聚合函数;

     由于 count(1) / max(day(last_day(submit_time))  中 分子count(1)用的是聚合函数,分母也必须用聚合函数,而函数day() 不是聚合函数,因此分母最终的逻辑为:max(day(last_day(submit_time)) 或min(day(last_day(submit_time))

方式二:with rollup

select coalesce(months,'2021汇总') as submit_month,
       count(question_id) as month_q_cnt,
       round(count(question_id)/max(days),3) as avg_day_cnt
from(select question_id,
            date_format(submit_time,'%Y%m') as months,
            day(last_day(submit_time)) as days
    from practice_record
    where year(submit_time)= '2021')  tmp1
group by months
with rollup;

上述代码拆解:

step1:利用date_format函数及day(last_day(submit_time)) 函数分别获取月份及当月的天数

select question_id,
       date_format(submit_time,'%Y%m') as months,
       day(last_day(submit_time)) as days
from practice_record
 where year(submit_time)= '2021'

step2:  利用 group by with rollup 实现分组加和,利用ifnull/coalesce函数进行null值判断及补全:coalesce(months,'2021汇总') as submit_month

最终的代码如下:

select coalesce(months,'2021汇总') as submit_month,
       count(question_id) as month_q_cnt,
       round(count(question_id)/max(days),3) as avg_day_cnt
from(select question_id,
            date_format(submit_time,'%Y%m') as months,
            day(last_day(submit_time)) as days
    from practice_record
    where year(submit_time)= '2021')  tmp1
group by months
with rollup;

group by with rollup具体使用案例见文章:

MySQL ——group by子句使用with rollup-CSDN博客MySQL ——group by子句使用with rolluphttps://blog.csdn.net/SHWAITME/article/details/136078305?spm=1001.2014.3001.5502

3 小结

   上述案例最关键的点在于:group by 分组后,select后面只能跟:

(1) groupby 分组的字段;

(2)常量;

(3) count()、 max()、 min()、avg()、sum()等聚合函数;


http://www.kler.cn/a/233183.html

相关文章:

  • 【插件】多断言 插件pytest-assume
  • MybatisPlus入门(十)MybatisPlus-逻辑删除和多记录操作
  • 蓝桥杯每日真题 - 第7天
  • 【JVM】关于JVM的内部原理你到底了解多少(八股文面经知识点)
  • 3D绘制动态爱心Matlab
  • 移动端【01】面试系统的MVVM重构实践
  • 【kafka】使用kafka client连接 kerberos认证的 kafka,scala版
  • 书生·浦语大模型第三课作业
  • Blender教程(基础)--试图的显示模式-22
  • TDengine用户权限管理
  • 图论:合适的环
  • 【Docker】了解Docker Desktop桌面应用程序,TA是如何管理和运行Docker容器(2)
  • Spring第三天
  • Vscode编译运行多个C++文件
  • Unity GC
  • 题目练习(生死时速2.0版)
  • C#既然数组长度不可改变,那么如何动态调整集合类型数组大小,以便添加或删除元素?
  • 学习通考试怎么搜题找答案? #学习方法#微信#其他
  • Gradle IDEA 乱码
  • 图灵之旅--二叉树堆排序
  • Android 判断通知是进度条通知
  • 生成式人工智能攻击的一年:2024
  • 【Mybatis】从0学习Mybatis(2)
  • Electron基本介绍
  • [职场] 如何通过运营面试_1 #笔记#媒体#经验分享
  • centos7.9 安装rabbitmq 3.6.15 集群