【牛客】SQL147 筛选限定昵称成就值活跃日期的用户

描述

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

|----|------|-----------|-------------|-------|-----|---------------------|
| id | uid | nick_name | achievement | level | job | register_time |
| 1 | 1001 | 牛客1号 | 1000 | 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 | 牛客5号 | 3000 | 7 | C++ | 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 |
| 3 | 1001 | 9002 | 2021-02-02 19:01:01 | 2021-02-02 19:30:01 | 87 |
| 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 |
| 6 | 1001 | 9002 | 2021-09-01 12:01:01 | (NULL) | (NULL) |
| 5 | 1001 | 9002 | 2021-09-05 19:01:01 | 2021-09-05 19:40:01 | 89 |
| 11 | 1002 | 9001 | 2020-01-01 12:01:01 | 2020-01-01 12:31:01 | 81 |
| 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 |
| 7 | 1002 | 9002 | 2021-05-05 18:01:01 | 2021-05-05 18:59:02 | 90 |
| 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) |
| 8 | 1003 | 9003 | 2021-02-06 12:01:01 | (NULL) | (NULL) |
| 9 | 1003 | 9001 | 2021-09-07 10:01:01 | 2021-09-07 10:31:01 | 89 |
| 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-02-01 11:31:01 | 84 |

题目练习记录表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 | 1003 | 8002 | 2021-09-01 19:38:01 | 80 |

请找到昵称以『牛客』开头『号』结尾、成就值在1200~2500之间,且最近一次活跃(答题或作答试卷)在2021年9月的用户信息。

由示例数据结果输出如下:

|------|-----------|-------------|
| uid | nick_name | achievement |
| 1002 | 牛客2号 | 1200 |

解释:昵称以『牛客』开头『号』结尾且成就值在1200~2500之间的有1002、1004;

1002最近一次试卷区活跃为2021年9月,最近一次题目区活跃为2021年9月;1004最近一次试卷区活跃为2021年8月,题目区未活跃。

因此最终满足条件的只有1002。

sql 复制代码
with cte1 as(
    select uid,start_time as active_time from exam_record
    union all
    select uid,submit_time from practice_record
),
cte2 as(
    select uid
    from
        (select uid,active_time,
        dense_rank() over(partition by uid order by active_time desc) as time_rank
        from cte1)t
    where time_rank=1 and left(active_time,7)='2021-09'
)

select
uid,nick_name,achievement
from
cte2 left join user_info
using (uid)
where nick_name like '牛客%号' 
and achievement>=1200 and achievement<=2500
相关推荐
笃行3503 小时前
国产化落地避坑 · 干货向|迁移后那条「测试能过、一上生产就查空」的 SQL,多半栽在 WHERE 函数顺序上
数据库
蓝田~3 小时前
MySQL慢查询怎么优化?B+Tree索引原理+MVCC读不阻塞写+EXPLAIN执行计划,从5秒到0.05秒
数据库·mysql
Herbert_hwt3 小时前
MySQL学习前言:关于MySQL的历史与当下学习的必要性
mysql
sunxunyong4 小时前
doris用户资源组配置
数据库
Cloud云卷云舒4 小时前
云卷云舒:HaishanDB的AI原生能力主要应用场景
数据库·人工智能·ai-native·haishandb·移动云长期记忆
时间的拾荒人4 小时前
MySQL 视图详解
android·数据库·mysql
qq_366086224 小时前
sql 查询中关于 null 值判断的问题
数据库·sql
测试修炼手册4 小时前
[测试技术] JUnit 6 入门与实战:断言、参数化与 Mockito
数据库·junit·sqlserver
吴声子夜歌5 小时前
Redis 3.x——集群故障转移
java·数据库·redis·集群
名不经传的养虾人5 小时前
从0到1:企业级AI项目迭代日记 Vol.71|系统显示正常,但实际上什么都没发生
数据库·人工智能·ai编程·ai工作流·企业ai