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

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

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

全部评论

相关推荐

找不到工作死了算了:没事的,雨英,hr肯主动告知结果已经超越大部分hr了
点赞 评论 收藏
分享
10-24 11:10
山西大学 Java
若梦难了:哥们,面试挂是很正常的。我大中厂终面挂,加起来快10次了,继续努力吧。
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务