本文主要是介绍【牛客】SQL148 筛选昵称规则和试卷规则的作答记录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
描述
现有用户信息表user_info(uid用户ID,nick_name昵称, achievement成就值, level等级, job职业方向, register_time注册时间):
id | uid | nick_name | achievement | level | job | register_time |
1 | 1001 | 牛客1号 | 1900 | 2 | 算法 | 2020-01-01 10:00:00 |
2 | 1002 | 牛客2号 | 1200 | 3 | 算法 | 2020-01-01 10:00:00 |
3 | 1003 | 牛客3号♂ | 2200 | 5 | 算法 | 2020-01-01 10:00:00 |
4 | 1004 | 牛客4号 | 2500 | 6 | 算法 | 2020-01-01 10:00:00 |
5 | 1005 | 牛客555号 | 2000 | 7 | C++ | 2020-01-01 10:00:00 |
6 | 1006 | 666666 | 3000 | 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 | C++ | hard | 60 | 2020-01-01 10:00:00 |
2 | 9002 | c# | hard | 80 | 2020-01-01 10:00:00 |
3 | 9003 | SQL | medium | 70 | 2020-01-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 | 2020-01-02 09:01:01 | 2020-01-02 09:21:59 | 80 |
2 | 1001 | 9001 | 2021-05-02 10:01:01 | (NULL) | (NULL) |
4 | 1001 | 9001 | 2021-06-02 19:01:01 | 2021-06-02 19:32:00 | 20 |
3 | 1001 | 9002 | 2021-02-02 19:01:01 | 2021-02-02 19:30:01 | 87 |
5 | 1001 | 9002 | 2021-09-05 19:01:01 | 2021-09-05 19:40:01 | 89 |
6 | 1001 | 9002 | 2021-09-01 12:01:01 | (NULL) | (NULL) |
11 | 1002 | 9001 | 2020-01-01 12:01:01 | 2020-01-01 12:31:01 | 81 |
16 | 1002 | 9001 | 2021-09-06 12:01:01 | 2021-09-06 12:21:01 | 80 |
17 | 1002 | 9001 | 2021-09-06 12:01:01 | (NULL) | (NULL) |
18 | 1002 | 9001 | 2021-09-07 12:01:01 | (NULL) | (NULL) |
7 | 1002 | 9002 | 2021-05-05 18:01:01 | 2021-05-05 18:59:02 | 90 |
12 | 1002 | 9002 | 2020-02-01 12:01:01 | 2020-02-01 12:31:01 | 82 |
13 | 1002 | 9002 | 2020-02-02 12:11:01 | 2020-02-02 12:31:01 | 83 |
9 | 1003 | 9001 | 2021-09-07 10:01:01 | 2021-09-07 10:31:01 | 89 |
8 | 1003 | 9003 | 2021-02-06 12:01:01 | (NULL) | (NULL) |
10 | 1004 | 9002 | 2021-08-06 12:01:01 | (NULL) | (NULL) |
14 | 1005 | 9001 | 2021-02-01 11:01:01 | 2021-02-01 11:31:01 | 84 |
15 | 1006 | 9001 | 2021-02-01 11:01:01 | 2021-09-01 11:31:01 | 84 |
找到昵称以"牛客"+纯数字+"号"或者纯数字组成的用户对于字母c开头的试卷类别(如C,C++,c#等)的已完成的试卷ID和平均得分,按用户ID、平均分升序排序。由示例数据结果输出如下:
uid | exam_id | avg_score |
1002 | 9001 | 81 |
1002 | 9002 | 85 |
1005 | 9001 | 84 |
1006 | 9001 | 84 |
解释:昵称满足条件的用户有1002、1004、1005、1006;c开头的试卷有9001、9002;
满足上述条件的作答记录中,1002完成9001的得分有81、80,平均分为81(80.5取整四舍五入得81);
1002完成9002的得分有90、82、83,平均分为85;
select
uid,exam_id,round(avg(score),0) as avg_score
from
exam_record left join user_info using(uid)
where
exam_id in (select exam_id from examination_info where tag like 'c%') and
submit_time is not null and
nick_name like '牛客%号' or nick_name rlike '^[0-9]+$'
group by uid,exam_id
order by uid,avg_score
这篇关于【牛客】SQL148 筛选昵称规则和试卷规则的作答记录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!