SQL29 连续两次作答试卷的最大时间窗

本文通过SQL查询技巧,分析了2021年那些至少连续两天作答试卷的用户,计算他们连续两次作答的最长间隔(days_window),并探讨他们在该时间段内的平均试卷完成数量(avg_exam_cnt)。数据结果按时间窗和平均做题数排序。
摘要由CSDN通过智能技术生成

SQL29 连续两次作答试卷的最大时间窗

1、函数

lead (str1,number) over(partition by str2 order by str3)

题目

现有试卷作答记录表exam_record(uid用户ID, exam_id试卷ID, start_time开始作答时间, submit_time交卷时间, score得分):
在这里插入图片描述
请计算在2021年至少有两天作答过试卷的人中,计算该年连续两次作答试卷的最大时间窗days_window,那么根据该年的历史规律他在days_window天里平均会做多少套试卷,按最大时间窗和平均做答试卷套数倒序排序。由示例数据结果输出如下:
在这里插入图片描述

select uid
    ,days_window
    ,round(days_window*exam_count/total_days,2) as avg_exam_cnt
from
(select 
    uid
    ,max(datediff(next_time,start_time))+1 days_window
    ,count(start_time) as exam_count
    ,datediff(max(start_time),min(start_time))+1 total_days
from
(select uid
    ,start_time
    ,lead(start_time,1) over(partition by uid order by start_time asc) as next_time
 from exam_record
where year(start_time)=2021)a 
group by uid
having datediff(max(start_time),min(start_time))+1>1)b
order by days_window desc,avg_exam_cnt desc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值