简体   繁体   English

在两个表中按日期分组的count(*)条记录

[英]count(*) records grouped by date across two tables

I've got two tables: 我有两个桌子:

emailLog table: emailLog表:

email           sendTime                   sourceTag
-------------------------------------------------------
yadda@aol.com   2016-11-17 09:14:37.213    WelcomeEmail
badda@aol.com   2016-11-16 09:14:37.213    WelcomeEmail
test@aol.com    2016-11-15 09:12:33.213    WelcomeEmail

registrations table: 注册表

email           dateRegistered             regSource
-------------------------------------------------------
yadda@aol.com   2016-11-17 09:14:37.213    WelcomeEmail
badda@aol.com   2016-11-16 09:14:37.213    WelcomeEmail
test@aol.com    2016-11-15 09:12:33.213    WelcomeEmail

I'm trying to do a combined query to show the COUNT() of people who received an email on a given date compared with the COUNT() of people who have registered on a given date 我正在尝试做一个组合查询,以显示在给定日期收到电子邮件的人的COUNT()与在给定日期注册电子邮件的人的COUNT()

I have gotten as far as this: 我已经做到了这一点:

SELECT
    CONVERT(varchar(10), sendTime, 120) as date,
    COUNT(*) as numberSent 
    FROM emailLog
WHERE sourceTag = 'WelcomeEmail'
AND
sendTime BETWEEN '20161110' and '20161120'
GROUP BY
    CONVERT(varchar(10), sendTime, 120)
ORDER BY DATE ASC;

Which gives me a list of the emails sent with a specific sourceTag, grouped by the date: 这给了我一个带有特定sourceTag发送的电子邮件的列表,按日期分组:

DATE                NUMBERSENT
2016-11-17          256
2016-11-18          136
2016-11-19          40
2016-11-20          118
2016-11-21          186

But I can't figure out how to join a sum of the registrations for that date + with that source, like: 但是我不知道如何将那个日期+的注册总和与该来源合并,例如:

DATE                NUMBERSENT    MEMBERSREGISTERED
2016-11-17          256           12
2016-11-18          136           24
2016-11-19          40            13
2016-11-20          118           2
2016-11-21          186           11

I have tried to do something like... 我试图做类似...

SELECT 
 (SELECT count(*) from emailLog) 
    as emailLogResults, 
 (select count(*) from registrations) 
    as registrationResults
    ...

but I am stuck after that. 但那之后我被困住了。 Any help very much appreciated 任何帮助非常感谢

To keep things simple and clear, i think, you can create 2 temporary tables. 为了使事情简单明了,我认为您可以创建2个临时表。 The first one will contain the list of the emails sent with a specific sourceTag, grouped by the date, as you already did. 与您已经做过的一样,第一个将包含带有特定sourceTag发送的电子邮件的列表,按日期分组。 And the second table will do the same thing for the registrations. 第二张表将对注册执行相同的操作。 You can than inner join the 2 temporary tables. 然后,您可以内部连接2个临时表。

You can use a full join (to get rows from the other table when are no rows for a specific date in either of the tables) with the two tables and then use conditional aggregation. 您可以对这两个表使用full join (当其中两个表中没有特定日期的行时,从另一个表中获取行),然后使用条件聚合。

SELECT
    COALESCE(CONVERT(varchar(10), e.sendTime, 120),CONVERT(varchar(10), r.dateRegistered, 120)) as date,
    COUNT(e.email) as numberSent,
    COUNT(r.email) as membersRegistered
FROM emailLog e
FULL JOIN registrations r ON e.sendTime = r.dateRegistered
AND e.sourceTag = 'WelcomeEmail'
AND r.regSource = 'WelcomeEmail'
AND e.sendTime BETWEEN '20161110' and '20161120' 
AND r.dateRegistered BETWEEN '20161110' and '20161120'
GROUP BY COALESCE(CONVERT(varchar(10), e.sendTime, 120),CONVERT(varchar(10), r.dateRegistered, 120))
ORDER BY DATE ASC

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

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