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

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

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

with xx as
(select user_info.uid,incomplete_cnt,incomplete_rate,level
from(
    select uid,sum(if(score is null,1,0)) incomplete_cnt,
    round(sum(if(score is null,1,0))/count(uid) ,3) incomplete_rate
    from exam_record
    group by uid
    ) t1
right join  user_info
on user_info.uid = t1.uid
)

(select uid,ifnull(incomplete_cnt,0) incomplete_cnt,ifnull(incomplete_rate,0) incomplete_rate
from xx
where  exists (select uid from xx where level=0 and incomplete_cnt>2) and level=0
)
union all
(select uid, incomplete_cnt, incomplete_rate
from xx
where  not exists (select uid from xx where level=0 and incomplete_cnt>2) and incomplete_cnt is not null
 )
order by incomplete_rate

exists用法:用于子查询中,满足条件返回TRUE

全部评论

相关推荐

07-02 22:46
门头沟学院 Java
码农索隆:hr:“管你投没投,先挂了再说”
点赞 评论 收藏
分享
06-02 15:53
阳光学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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