题解 | #异常的邮件概率#

异常的邮件概率

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

大致思路是:

type='no_completed'的行数 除以 以日期分组之后的行数

  1. 排除黑名单中的用户,关键是黑名单的用户既不能发邮件,也不能收邮件(我一开始就是只看到不能发邮件)
	select type,date from email e 
        join user u1 ON (e.send_id = u1.id and u1.is_blacklist != 1)
        join user u2 on (e.receive_id = u2.id and u2.is_blacklist != 1)
  1. 排除之后,需要计算type='no_completed'的行数

这里我的想法是将type='completed'的值改为null,然后count(type)即可得到所要的行数

  • 这里先将type='completed'的值改为null
	select 
        (case t.type
            when 'completed' then type = null
            else type
            end
        )as s,t.date
    from
        (select type,date from email e 
        	join user u1 ON (e.send_id = u1.id and u1.is_blacklist != 1)
        	join user u2 on (e.receive_id = u2.id and u2.is_blacklist != 1)
        ) t
  1. 拿到type='no_completed'的行数剩下的就简单了
select 
    d.date,
    round(count(d.s)*1.0/count(date),3) p
from 
    (select 
        (case t.type
            when 'completed' then type = null
            else type
            end
        )as s,t.date
    from
        (select type,date from email e 
        	join user u1 ON (e.send_id = u1.id and u1.is_blacklist != 1)
        	join user u2 on (e.receive_id = u2.id and u2.is_blacklist != 1)
        ) t
    )d
group by d.date 
全部评论

相关推荐

12-27 22:35
门头沟学院 Java
点赞 评论 收藏
分享
溱元:前端每年固定死几次,看两集广告就复活了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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