0

我有一张我发送的电子邮件表格以及我发送电子邮件的日期。

-----------------------------
|   email   |   date_sent   |
-----------------------------
|  a@a.com  |  2013-01-10   | 
|  a@a.com  |  2013-01-10   |
|  a@a.com  |  2013-01-10   |
|  b@b.com  |  2013-01-10   |
|  a@a.com  |  2013-01-11   |
|  c@c.com  |  2013-01-11   |
-----------------------------

对于给定的日期,我需要计算发送的电子邮件总数、今天的唯一地址以及以前未出现的唯一地址。

第一部分很简单:

SELECT count(email) emails_sent, count(distinct email) unique_users
from sent_emails where date_sent = '2013-01-10';

我无法收到以前未出现过的电子邮件。可能吗?

我尝试了许多不同的方法,但几乎没有成功。例如:

SELECT count(email) emails_sent, count(distinct email) unique_users,
sum( (select count(email) from sent_emails se
where se.date_sent < date_sent 
and se.email = email)) new_emailed_users
from sent_emails where date_sent = '2013-01-10';

我确信这是可能的。

这就是我预计 2013-01-10 的结果:

--------------------------------------------------
| emails_sent | unique_users | new_emailed_users |
--------------------------------------------------
|      4      |      2       |         2         |
--------------------------------------------------

这就是我预计 2013-01-11 的结果:

--------------------------------------------------
| emails_sent | unique_users | new_emailed_users |
--------------------------------------------------
|      2      |      2       |         1         |
--------------------------------------------------

谢谢你的帮助

4

2 回答 2

0

没有子查询,提高速度

select
count(if(date_sent='2013-01-10',email, null)) as emails,
count(distinct if(date_sent='2013-01-10',email, null)) as uniques,
count(distinct email) as total_unique_email
from sent_emails
于 2013-10-11T12:59:51.847 回答
-1

这是我的解决方案。不确定它是最有效的:

SELECT count(email) emails_sent, count(distinct email) unique_users,
count(distinct if(not exists(select 1 from sent_emails where  date_sent < se.date_sent and se.email = email ),email,null)) new_emailed_users
from sent_emails se where date_sent = '2013-01-10';
于 2013-10-11T14:29:05.643 回答