SQL259. 异常的邮件概率
描述
现在有一个需求,让你统计正常用户发送给正常用户邮件失败的概率:id | send_id | receive_id | type | date |
1 2 3 4 5 6 | 2 1 1 3 3 4 | 3 3 4 1 4 1 | completed completed no_completed completed completed completed | 2020-01-11 2020-01-11 2020-01-11 2020-01-12 2020-01-12 2020-01-12 |
id | is_blacklist |
1 2 3 4 | 0 1 0 0 |
date | p |
2020-01-11 2020-01-12 | 0.500 0.000 |
示例1
输入:
drop table if exists email; drop table if exists user; CREATE TABLE `email` ( `id` int(4) NOT NULL, `send_id` int(4) NOT NULL, `receive_id` int(4) NOT NULL, `type` varchar(32) NOT NULL, `date` date NOT NULL, PRIMARY KEY (`id`)); CREATE TABLE `user` ( `id` int(4) NOT NULL, `is_blacklist` int(4) NOT NULL, PRIMARY KEY (`id`)); INSERT INTO email VALUES (1,2,3,'completed','2020-01-11'), (2,1,3,'completed','2020-01-11'), (3,1,4,'no_completed','2020-01-11'), (4,3,1,'completed','2020-01-12'), (5,3,4,'completed','2020-01-12'), (6,4,1,'completed','2020-01-12'); INSERT INTO user VALUES (1,0), (2,1), (3,0), (4,0);
输出:
2020-01-11|0.500 2020-01-12|0.000
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3364KB, 提交时间: 2021-09-11
select date, round(sum(type = "no_completed")*1.0 / 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;
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3456KB, 提交时间: 2021-11-22
select e.date, round ( sum(case e.type when 'completed' then 0 else 1 end)*1.0 / count(type),3 ) as p from email e join user u on e.send_id=u.id and u.is_blacklist !=1 join user u1 on e.receive_id=u1.id and u1.is_blacklist !=1 group by e.date order by e.date
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3456KB, 提交时间: 2021-09-10
select a.date, round(sum(case when a.type='no_completed' then 1 else 0 end)*1.0/count(a.type),3) as p from email a join user b on a.send_id=b.id join user c on a.receive_id=c.id where b.is_blacklist='0' and c.is_blacklist='0' group by 1 order by 1
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3492KB, 提交时间: 2021-09-07
SELECT date, ROUND(SUM(CASE WHEN type = 'completed' THEN 0 ELSE 1 END)*1.0/COUNT(*), 3) FROM email e WHERE send_id IN (SELECT id FROM user WHERE is_blacklist = 0) AND receive_id IN (SELECT id FROM user WHERE is_blacklist = 0) GROUP BY date ORDER BY date
Sqlite 解法, 执行用时: 10ms, 内存消耗: 3496KB, 提交时间: 2021-09-10
select date, round(sum(case when type="completed" then 0 else 1 end )*1.0/count(type),3) as co from email where send_id in( select id from user where is_blacklist=0 )and receive_id in( select id from user where is_blacklist=0 )group by date