简体   繁体   English

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

[英]SQL aggregation select using SUM and COUNT on different tables

I have a table emails 我有一张桌子的电子邮件

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

table2 is responses table2是响应

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

I want a result with the following format: 我想要具有以下格式的结果:

Month total_number_of_subscribers_sent total_responded
2013-01 1335 2
.
.

this is my query: 这是我的查询:

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')

it works ok with total_responded, but the total_sent goes crazy in millions, obviously because the resultant join table has the redundant values. 它可以与total_responded一起正常工作,但是total_sent变得疯狂了几百万,这显然是因为结果联接表具有冗余值。

So basically can I do a SUM and COUNT in the same query on separate tables ? 因此,基本上可以在同一查询中对不同的表进行SUM和COUNT吗?

If you want to count duplicates in each table, then the query is a little complicated. 如果要计算每个表中的重复项,则查询有点复杂。

You need to aggregate the sends and responses separately, before joining them together. 在将它们连接在一起之前,需要分别汇总发送和响应。 The join is on the date, which necessarily comes from the "sent" information: 加入日期是日期,该日期必然来自“已发送”信息:

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`;

The apparent fact that your responses have no link to the "sent" information -- not even the date -- suggests a real problem with your operations and data. 您的回复与“已发送”信息甚至日期都没有链接的明显事实表明您的操作和数据确实存在问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM