SQL140 未完成率较高的50%用户近
SQL140 未完成率较高的50%用户近三个月答卷情况
1、首先先统计出SQL试卷所有用户的未答数,答题总数,未完成率
2、通过给增加排名来取出50%的用户。使其按照未完成率降序,排名小于(最大名次+1)/2(+1是为了向上取整)的就是前50%的用户。
3、使用不跳过排名函数DENSE_RANK()来取出最近三个月的答卷情况
4、将所有的条件结合到一起,排好序,输出所需即可
with t1 as(
select exam_record.uid uid
,sum(if(submit_time is null,1,0)) incomplete_cnt,count(exam_record.uid) total_cnt
,sum(if(submit_time is null,1,0))/count(exam_record.uid) incomplete_rate
,row_number() over(order by sum(if(submit_time is null,1,0))/count(exam_record.uid) desc) ranking -- 统计SQL试卷上用户的未完成数、作答总数、未完成率,用户id
from
exam_record left join examination_info
on examination_info.exam_id=exam_record.exam_id
where tag='SQL' group by uid
),t2 as(
select uid from t1
where ((select max(ranking) from t1 t)+1)/2>=ranking -- 根据未完成率选出50%的用户
),t3 as (
SELECT uid, submit_time, DATE_FORMAT(start_time, "%Y%m") as start_month,
DENSE_RANK() over(
ORDER BY DATE_FORMAT(start_time, "%Y%m") DESC
) as start_month_rank -- 按作答月份降序编号,选出近三个月的月份
FROM exam_record
)
SELECT uid, start_month, COUNT(1) as exam_cnt, COUNT(submit_time) as complete_cnt
FROM t3
WHERE start_month_rank <= 3 -- 选出近三个月的月份
AND uid in (select uid from t2) AND uid IN (SELECT uid FROM user_info WHERE `level`>=6)
GROUP BY uid, start_month
ORDER BY uid, start_month;
1、首先先统计出SQL试卷所有用户的未答数,答题总数,未完成率
2、通过给增加排名来取出50%的用户。使其按照未完成率降序,排名小于(最大名次+1)/2(+1是为了向上取整)的就是前50%的用户。
3、使用不跳过排名函数DENSE_RANK()来取出最近三个月的答卷情况
4、将所有的条件结合到一起,排好序,输出所需即可
with t1 as(
select exam_record.uid uid
,sum(if(submit_time is null,1,0)) incomplete_cnt,count(exam_record.uid) total_cnt
,sum(if(submit_time is null,1,0))/count(exam_record.uid) incomplete_rate
,row_number() over(order by sum(if(submit_time is null,1,0))/count(exam_record.uid) desc) ranking -- 统计SQL试卷上用户的未完成数、作答总数、未完成率,用户id
from
exam_record left join examination_info
on examination_info.exam_id=exam_record.exam_id
where tag='SQL' group by uid
),t2 as(
select uid from t1
where ((select max(ranking) from t1 t)+1)/2>=ranking -- 根据未完成率选出50%的用户
),t3 as (
SELECT uid, submit_time, DATE_FORMAT(start_time, "%Y%m") as start_month,
DENSE_RANK() over(
ORDER BY DATE_FORMAT(start_time, "%Y%m") DESC
) as start_month_rank -- 按作答月份降序编号,选出近三个月的月份
FROM exam_record
)
SELECT uid, start_month, COUNT(1) as exam_cnt, COUNT(submit_time) as complete_cnt
FROM t3
WHERE start_month_rank <= 3 -- 选出近三个月的月份
AND uid in (select uid from t2) AND uid IN (SELECT uid FROM user_info WHERE `level`>=6)
GROUP BY uid, start_month
ORDER BY uid, start_month;
全部评论
相关推荐
09-24 12:03
南京大学 用户运营 末日黄昏:那好,请你讲一下福星的原理。福星是怎么实现的?有看过福星的源码吗?有自己上手去试试福星相关的项目吗?在你的项目中,你觉得还有什么可以优化的地方?你觉得福星未来的发展是怎么样的?和我们的业务能有什么结合?
点赞 评论 收藏
分享