【牛客】SQL134 满足条件的用户的试卷完成数和题目练习数

描述

现有用户信息表user_info(uid用户ID,nick_name昵称, achievement成就值, level等级, job职业方向, register_time注册时间):

|----|------|-----------|-------------|-------|-----|---------------------|
| id | uid | nick_name | achievement | level | job | register_time |
| 1 | 1001 | 牛客1号 | 3100 | 7 | 算法 | 2020-01-01 10:00:00 |
| 2 | 1002 | 牛客2号 | 2300 | 7 | 算法 | 2020-01-01 10:00:00 |
| 3 | 1003 | 牛客3号 | 2500 | 7 | 算法 | 2020-01-01 10:00:00 |
| 4 | 1004 | 牛客4号 | 1200 | 5 | 算法 | 2020-01-01 10:00:00 |
| 5 | 1005 | 牛客5号 | 1600 | 6 | C++ | 2020-01-01 10:00:00 |
| 6 | 1006 | 牛客6号 | 2000 | 6 | C++ | 2020-01-01 10:00:00 |

试卷信息表examination_info(exam_id试卷ID, tag试卷类别, difficulty试卷难度, duration考试时长, release_time发布时间):

|----|---------|-----|------------|----------|---------------------|
| id | exam_id | tag | difficulty | duration | release_time |
| 1 | 9001 | SQL | hard | 60 | 2021-09-01 06:00:00 |
| 2 | 9002 | C++ | hard | 60 | 2021-09-01 06:00:00 |
| 3 | 9003 | 算法 | medium | 80 | 2021-09-01 10:00:00 |

试卷作答记录表exam_record(uid用户ID, exam_id试卷ID, start_time开始作答时间, submit_time交卷时间, score得分):

|----|------|---------|---------------------|---------------------|-------|
| id | uid | exam_id | start_time | submit_time | score |
| 1 | 1001 | 9001 | 2021-09-01 09:01:01 | 2021-09-01 09:31:00 | 81 |
| 2 | 1002 | 9002 | 2021-09-01 12:01:01 | 2021-09-01 12:31:01 | 81 |
| 3 | 1003 | 9001 | 2021-09-01 19:01:01 | 2021-09-01 19:40:01 | 86 |
| 4 | 1003 | 9002 | 2021-09-01 12:01:01 | 2021-09-01 12:31:51 | 89 |
| 5 | 1004 | 9001 | 2021-09-01 19:01:01 | 2021-09-01 19:30:01 | 85 |
| 6 | 1005 | 9002 | 2021-09-01 12:01:01 | 2021-09-01 12:31:02 | 85 |
| 7 | 1006 | 9003 | 2021-09-07 10:01:01 | 2021-09-07 10:21:01 | 84 |
| 8 | 1006 | 9001 | 2021-09-07 10:01:01 | 2021-09-07 10:21:01 | 80 |

题目练习记录表practice_record(uid用户ID, question_id题目ID, submit_time提交时间, score得分):

|----|------|-------------|---------------------|-------|
| 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 | 1004 | 8001 | 2021-08-02 19:38:01 | 70 |
| 6 | 1004 | 8002 | 2021-08-02 19:48:01 | 90 |
| 7 | 1001 | 8002 | 2021-08-02 19:38:01 | 70 |
| 8 | 1004 | 8002 | 2021-08-02 19:48:01 | 90 |
| 9 | 1004 | 8002 | 2021-08-02 19:58:01 | 94 |
| 10 | 1004 | 8003 | 2021-08-02 19:38:01 | 70 |
| 11 | 1004 | 8003 | 2021-08-02 19:48:01 | 90 |
| 12 | 1004 | 8003 | 2021-08-01 19:38:01 | 80 |

请你找到高难度SQL试卷得分平均值大于80并且是7级的红名大佬,统计他们的2021年试卷总完成次数和题目总练习次数,只保留2021年有试卷完成记录的用户。结果按试卷完成数升序,按题目练习数降序。

示例数据输出如下:

|------|----------|--------------|
| uid | exam_cnt | question_cnt |
| 1001 | 1 | 2 |
| 1003 | 2 | 0 |

解释:用户1001、1003、1004、1006满足高难度SQL试卷得分平均值大于80,但只有1001、1003是7级红名大佬;1001完成了1次试卷1001,练习了2次题目;1003完成了2次试卷9001、9002,未练习题目(因此计数为0)

sql 复制代码
with cte1 as(
    select uid
    from
    exam_record left join examination_info using(exam_id)
    left join user_info using(uid)
    where difficulty='hard' and tag='SQL' and level=7
    group by uid
    having avg(score)>80
),
cte2 as(
    select uid,count(exam_id) as exam_cnt
    from
    cte1 left join  exam_record using(uid)
    where left(submit_time,4)=2021
    group by uid
),
cte3 as(
    select uid,count(question_id) as question_cnt
    from
    cte1 left join practice_record using(uid)
    where date_format(submit_time,'%Y')=2021
    group by uid
)

select uid,exam_cnt,ifnull(question_cnt,0)
from
cte2 left join cte3 using(uid)
order by exam_cnt,question_cnt desc
相关推荐
B站_计算机毕业设计之家2 分钟前
深度学习:Yolo水果检测识别系统 深度学习算法 pyqt界面 训练集测试集 深度学习 数据库 大数据 (建议收藏)✅
数据库·人工智能·python·深度学习·算法·yolo·pyqt
赵文宇2 小时前
站在MySQL肩膀上快速入门PostgreSQL,开源社区最喜爱的关系数据库
mysql·postgresql
wei_shuo2 小时前
全场景自动化 Replay 技术:金仓 KReplay 如何攻克数据库迁移 “难验证“ 难题
数据库·自动化·king base
葡萄城技术团队2 小时前
打破误解!MongoDB 事务隔离级别深度实测:快照隔离竟能防住 8 种异常?
数据库
Gold Steps.2 小时前
数据库正常运行但是端口变成了0?
数据库·mysql
杂亿稿2 小时前
增删改查操作
数据库
Code_Geo2 小时前
在postgres数据库中Postgres FDW 全面详解
数据库·fdw
QT 小鲜肉3 小时前
【个人成长笔记】将Try Ubuntu里面配置好的文件系统克隆在U盘上(创建一个带有持久化功能的Ubuntu Live USB系统)
linux·开发语言·数据库·笔记·ubuntu
LWy6104263 小时前
数据库库、表的创建及处理
数据库
Jay_Franklin3 小时前
Python中使用sqlite3模块和panel完成SQLite数据库中PDF的写入和读取
数据库·笔记·python·pycharm·sqlite·pdf·py