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

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

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

稍微有点复杂,分成是否有0级用户未完成数大于2的两种情况union all起来
1、先求出0级用户未完成数大于2的个数,大于0说明是第一种情况,只需要输出0级用户
      需要注意的是0级用户可能没作答记录,用用户表左连接,只有start_time和submit_time都为null时才是没有作答记录的,未完成数记为0
      start_time 不为null 且 submit_time 为null时才是未完成,记数1
2、0级用户未完成数大于2的个数,等于0说明是第二种情况,直接求作答用户的未完成情况即可
select t1.uid,sum(case when submit_time is null and start_time is null then 0
                       when submit_time is null and start_time is not null then 1 end) as incomplete_cnt,
       round(sum(case when submit_time is null and start_time is null then 0
                       when submit_time is null and start_time is not null then 1 end)/count(1),3) as incomplete_rate
from user_info t1
left join exam_record t2 on t1.uid = t2.uid
where level = 0 
and 0 < (
          select count(1)  as cn 
          from (
                  select t1.uid,count(1) as incomplete_cnt
                  from user_info t1
                  join exam_record t2
                  on t1.uid = t2.uid
                  where level = 0
                  and submit_time is null
                  group by t1.uid
                  having count(1) > 2
              ) t
         )
group by t1.uid
union all
select uid,sum(if(submit_time is null,1,0)) as incomplete_cnt,
       round(sum(if(submit_time is null,1,0))/count(1),3) as incomplete_rate
from exam_record
where 0 = (
          select count(1)  as cn 
          from (
                  select t1.uid,count(1) as incomplete_cnt
                  from user_info t1
                  join exam_record t2
                  on t1.uid = t2.uid
                  where level = 0
                  and submit_time is null
                  group by t1.uid
                  having count(1) > 2
              ) t
         )
group by uid
order by incomplete_rate


全部评论

相关推荐

06-20 17:42
东华大学 Java
凉风落木楚山秋:要是在2015,你这简历还可以月入十万,可惜现在是2025,已经跟不上版本了
我的简历长这样
点赞 评论 收藏
分享
龙珠传说:nb,公务员解约不需要支付违约金吧
点赞 评论 收藏
分享
06-27 12:30
延安大学 C++
实习+外包,这两个公司底层融为一体了,如何评价呢?
一表renzha:之前面了一家外包的大模型,基本上都能答出来,那面试官感觉还没我懂,然后把我挂了,我都还没嫌弃他是外包,他把我挂了……
第一份工作能做外包吗?
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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