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

异常的邮件概率

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

我的方法:n多个子查询,t1表:group by以后的失败次数,t2表:group by以后的总次数,join连接相除得到概率,子查询去除黑名单用户

with tmp as(
select * from email 
where send_id not in (select id from user where is_blacklist=1)
and receive_id not in (select id from user where is_blacklist=1)
)
select t1.date,round(f/s,3)from (select date,count(id)f from tmp
where type='no_completed'
group by date)t1 join
(select date,count(id)s from tmp 
group by date
)t2 on t1.date=t2.date
order by t1.date

法二:sum+case when算失败次数,join去除黑名单用户

select email.date, round(
sum(case email.type when'completed' then 0 else 1 end)*1.0/count(email.type),3
) as p
from email
join user as u1 on (email.send_id=u1.id and u1.is_blacklist=0)
join user as u2 on (email.receive_id=u2.id and u2.is_blacklist=0)
group by email.date order by email.date;

法三:sum直接算失败次数

select date, round(sum(type = "no_completed") / count(*), 3) as p
from email as t1
join user as t2 on t1.send_id = t2.id
join user as t3 on t1.receive_id = t3.id
where t2.is_blacklist = 0 and t3.is_blacklist = 0
group by date
order by date;
SQL题解 文章被收录于专栏

主要是为自己做个笔记

全部评论

相关推荐

小浪_Coding:找硬件测试,也可兼顾软测欧, 简历还可以的 ,注意排版,项目写的有条理一点, 然后个人技能多加点, 润色好简历之后就开始沟通海投了,深圳,东莞这边做硬件相关的公司还不少, 医疗类,仪器类的都可以尝试
点赞 评论 收藏
分享
05-27 14:57
西北大学 golang
强大的社畜在走神:27届真不用急,可以搞点项目、竞赛再沉淀沉淀,我大二的时候还在天天打游戏呢
投递华为等公司10个岗位
点赞 评论 收藏
分享
07-10 13:59
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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