· 有一个邮件(email)表,id为主键, type是枚举类型,枚举成员为(completed,no_completed),completed代表邮件发送是成功的,no_completed代表邮件是发送失败的
· 用户(user)表,id为主键,is_blacklist为0代表为正常用户,is_blacklist为1代表为黑名单用户
1、每一个日期里面,正常用户发送给正常用户邮件失败的概率
2、结果保留到小数点后面3位(3位之后的四舍五入)
3、按照日期升序排序
上面例子查询结果如下:
根据题目要求,需要求正常用户发送到正常用户的失败率,所以先连接user表求出正常用户
select xxx from email as e join user as u1 on e.send_id = u1.id and u1.is_blacklist = 0 join user as u2 on e.receive_id = u2.id and u2.is_blacklist = 0
然后求出失败的概率 = 失败的次数 / 总次数
总次数为count(e.type);失败的次数可以使用case..when..函数去计算,得sum (case when e.type = completed then 0 else 1 end)
select round(1.0 * sum(case when e.type = 'completed' then 0 else 1 end) / count(e.type), 3) as p from email as e join user as u1 on e.send_id = u1.id and u1.is_blacklist = 0 join user as u2 on e.receive_id = u2.id and u2.is_blacklist = 0
联立其它条件可得
select e.date, round(1.0 * sum(case when e.type = 'completed' then 0 else 1 end) / count(e.type), 3) as p from email as e join user as u1 on e.send_id = u1.id and u1.is_blacklist = 0 join user as u2 on e.receive_id = u2.id and u2.is_blacklist = 0 group by e.date order by e.date;