1

我有一张桌子的电子邮件

id date sent_to
1 2013-01-01 345
2 2013-01-05 990
3 2013-02-05 1000

表 2 是响应

email_id email  response
1   xyz@email.com   xxxx
1   xyzw@email.com  yyyy
.
.
.

我想要具有以下格式的结果:

Month total_number_of_subscribers_sent total_responded
2013-01 1335 2
.
.

这是我的查询:

SELECT
DATE_FORMAT(e.date, '%Y-%m')AS `Month`,
    count(*) AS total_responded,
SUM(e.sent_to) AS total_sent
FROM
    responses r
LEFT JOIN emails e ON e.id = r.email_id 
WHERE
e.date > '2012-12-31' AND e.date < '2013-10-01'
GROUP BY
    DATE_FORMAT(e.date, '%Y %m')

它可以与 total_responded 一起使用,但 total_sent 会以数百万计发疯,显然是因为生成的连接表具有冗余值。

所以基本上我可以在不同表的同一查询中执行 SUM 和 COUNT 吗?

4

1 回答 1

0

如果要计算每个表中的重复项,则查询有点复杂。

在将它们连接在一起之前,您需要分别聚合发送和响应。加入日期,这必然来自“发送”的信息:

select r.`Month`, coalesce(total_sent, 0) as total_sent, coalesce(total_emails, 0) as total_emails,
       coalesce(total_responses, 0) as total_responses,
       coalesce(total_email_responses, 0) as total_email_responses
from (select DATE_FORMAT(e.date, '%Y-%m') as `Month`,
             count(*) as total_sent, count(distinct email) as total_emails
      from emails e
      where e.date > '2012-12-31' AND e.date < '2013-10-01'
      group by DATE_FORMAT(r.date, '%Y-%m') 
     ) e left outer join
     (select DATE_FORMAT(e.date, '%Y-%m') as `Month`,
             count(*) as total_responses, count(distinct r.email) as total_email_responses
      from emails e join
           responses r
           on e.email = r.email
      where e.date > '2012-12-31' AND e.date < '2013-10-01'
     ) r
     on e.`Month` = r.`Month`;

您的回复与“已发送”信息(甚至日期都没有)没有任何联系这一明显事实表明您的操作和数据存在真正的问题。

于 2013-09-10T21:29:25.453 回答