I've got two tables:
emailLog table:
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
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:
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. The first one will contain the list of the emails sent with a specific sourceTag, grouped by the date, as you already did. And the second table will do the same thing for the registrations. You can than inner join the 2 temporary tables.
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.