题解 | 本题难点:Exists

根据指定记录是否存在输出不同情况

http://www.nowcoder.com/practice/f72d3fc27dc14f3aae76ee9823ccca6b

-- 第一步 做出所有人的两个指标
with t as 
(
    select t.uid,level,sum(start_time is not null and submit_time is null) as incomplete_cnt,
        round(sum(start_time is not null and submit_time is null)/count(1),3) as incomplete_rate,
        count(exam_id) as num
    from user_info t
    left join exam_record t1 on t.uid = t1.uid
    group by t.uid
)
-- 第二步 冲
select uid,incomplete_cnt,incomplete_rate
from t
where EXISTS (
    select uid from t where level=0 and incomplete_cnt>2
) and level=0
union ALL
select uid,incomplete_cnt,incomplete_rate
from t
where not EXISTS (
    select uid from t where level=0 and incomplete_cnt>2
) and num>0
order by incomplete_rate



全部评论
#step1 先做出所有人试卷未完成数和未完成率 with 0_recore as ( SELECT ui.uid ,ui.level ,sum(er.start_time is not null and er.submit_time is null) incomplete_cnt ,round(sum(er.start_time is not null and er.submit_time is null)/COUNT(*),3) incomplete_rate ,count(er.exam_id) unans_num from user_info ui left join exam_record er using(uid) group by ui.uid,ui.level ) select uid,incomplete_cnt,incomplete_rate from 0_recore where EXISTS ( select uid from 0_recore where level = 0 and unans_num >2 ) and level = 0 union all select uid,incomplete_cnt,incomplete_rate from 0_recore where not EXISTS ( select uid from 0_recore where level = 0 and unans_num >2 ) and unans_num > 0 order by incomplete_rate; 请问下为啥自测通过提交不通过,我是照着您学的然后自己写的
1 回复 分享
发布于 2022-04-08 11:10
老哥,为什么你这题要start_time is not null 和 submit_time is null,直接一个start_time is not null 不行吗?
1 回复 分享
发布于 2021-12-25 16:28
start_time is not null and submit_time is null 这样写不就把没做过试卷的也排除了吗?
点赞 回复 分享
发布于 2023-03-04 17:39 江苏
请问count(1)是啥意思啊?未完成率的分母不应该是count(start_time)吗?
点赞 回复 分享
发布于 2022-04-01 09:33

相关推荐

uu们,拒offer时hr很生气怎么办我哭死
爱睡觉的冰箱哥:人家回收你的offer,或者oc后没给你发offer的时候可不会愧疚你,所以你拒了也没必要愧疚他。
点赞 评论 收藏
分享
zYvv:双一流加大加粗再标红,然后广投。主要是获奖荣誉不够,建议开始不用追求大厂,去别的厂子刷下实习。
点赞 评论 收藏
分享
评论
28
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务