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

异常的邮件概率

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

最直接的思考方式

select

t1.date,

round(t1.col1/t2.col2,3) as p

from(

select date,count(1) col1

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)

and type = 'no_completed'

group by date) t1

join

(select date,count(1) as col2 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) group by date) t2

on

t1.date = t2.date;

使用case when else end 来做 先过滤掉黑名单

select

date,

round(sum(case type when 'no_completed' then 1 else 0 end) /count(1) ,3) as p

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)

group by date;

应该还可以使用if

select
date,
round(sum(if(type = 'no_completed' ,1,0)) /count(1) ,3) as p
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)
group by date;

全部评论

相关推荐

爱睡觉的冰箱哥:你是我今晚见过的最美的牛客女孩
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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