1
我有一個表電子郵件SQL聚合選擇使用SUM和COUNT在不同的表
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 [email protected] xxxx
1 [email protected] 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在單獨的表上相同的查詢?
由於您的「表格」示例沒有顯示blastid列如何將它們連接在一起,因此無法確定您想要獲取的內容。 –
我編輯過它 – user2766524