题解 | #异常的邮件概率#
异常的邮件概率
http://www.nowcoder.com/practice/d6dd656483b545159d3aa89b4c26004e
采用简单的in判断是否正常邮件,效率不怎么好
思路:
1、找出正常邮件,id 不是 is_blacklist id;
2、正常且失败邮件,type = 'no_completed';
3、保留3位小数,round函数;
4、按照日期分组,group by date。
select
e.date
,round(( -- 三位小数
select -- 正常且失败邮件数
count(*) -- 统计行,一行一个邮件
from email
where send_id not in ( -- 发送者id非黑客id
select id
from user
where is_blacklist = 1
)
and receive_id not in ( -- 接收者id非黑客id
select id
from user
where is_blacklist = 1
)
and type = 'no_completed' -- 接收失败邮件
and date = e.date -- **date必须是分组日期,不然会全部统计
)/(
select
count(*)
from email
where send_id not in (-- 发送者id非黑客id
select id
from user
where is_blacklist = 1
)
and receive_id not in (-- 接收者id非黑客id
select id
from user
where is_blacklist = 1
)
and date = e.date -- **date必须是分组日期,不然会全部统计
),3) p
from email e
GROUP by date -- 按照日期分组