SQL聚合在不同的表上使用SUM和COUNT进行选择

时间:2013-09-10 21:01:13

标签: mysql sql

我有一个表格电子邮件

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

table2是回复

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吗?

1 个答案:

答案 0 :(得分: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`;

显而易见的事实是,您的回复与“已发送”信息没有关联 - 甚至不是日期 - 表明您的操作和数据存在实际问题。