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

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

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

with t1 as ( -- 形成一张大表,包含所有uid的计算要素
select uid,count(t.start_time) as num,
case when count(t.start_time)=0 then 0 else count(submit_time is null or null) end as incomplete_cnt
from user_info left join exam_record t using(uid)
group by uid)

SELECT uid, incomplete_cnt,ifnull(round(incomplete_cnt/num,3),round(0,3)) as incomplete_rate from t1
where -- 关键在于条件中使用case when来控制uid
case when  (select count(1) from t1 join user_info using(uid) where incomplete_cnt>2 and level=0 ) >0
            then uid in (select uid from user_info where level=0)
            else uid in (select uid from exam_record ) end
order by incomplete_rate

关键点在于思路
1、先构建大表,然后根据判断中使用 case when 来控制uid
2、where 中跟case when

全部评论

相关推荐

09-27 00:29
东北大学 Java
伟大的麻辣烫:查看图片
阿里巴巴稳定性 75人发布 投递阿里巴巴等公司10个岗位
点赞 评论 收藏
分享
扭转乾坤_:现在企业都是学华为,一直通过丢池子里,最后捞
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务