So what I'm trying to do here is get a report on how many emails (with a MailChimp like app) were sent by different users, but I want two different metrics in one query. I want to know how many individual emails were sent by each user. Meaning if they sent 3 emails to 100 contacts each, that would display 300. But I also want to know how many unique emails were sent, meaning that would display 3.
I'd like to get something that looks like:
-------------------------------------------------------------
| Full Name | Username | Total Sent | Unique Mails |
|-------------|-----------------|------------|--------------|
| John Doe | jdoe@mail.com | 12000 | 4 |
| James Smith | jsmith@mail.com | 6000 | 12 |
| Jane Jones | jjones@mail.com | 4000 | 2 |
| ... | ... | ... | ... |
-------------------------------------------------------------
So I could know that John sends a few emails to a lot of contacts while James sends more emails to fewer contacts.
Here's what my query looks like. I've changed the table and column names, but this is otherwise an exact representation of what it is.
SELECT
CONCAT(Usernames.FirstName, ' ', Usernames.LastName) AS 'Full Name',
Usernames.Username,
COUNT(Sent_Mail_Contacts.IDContact) AS `Total Sent`,
COUNT(Mass_Mail.IDMass_Mail) AS `Individual E-Mails`
FROM Usernames
LEFT JOIN Sent_Mail_Contacts ON Usernames.Username = Sent_Mail_Contacts.Username
LEFT JOIN Mass_Mail ON Usernames.Username = Mass_Mail.Username
GROUP BY Usernames.Username
ORDER BY `Total Sent`
I have a table with Usernames, a table with individual contacts reached by which emails and a table with unique emails.
So does my query make sense or not? Is this even possible? Because right now when I run it, it gives me something like this:
-------------------------------------------------------------
| Full Name | Username | Total Sent | Unique Mails |
|-------------|-----------------|------------|--------------|
| John Doe | jdoe@mail.com | 12000 | 12000 |
| James Smith | jsmith@mail.com | 6000 | 6000 |
| Jane Jones | jjones@mail.com | 4000 | 4000 |
| ... | ... | ... | ... |
-------------------------------------------------------------
I just gives me the same number in both columns and takes 7 minutes to process.
Here is an example of what the 3 tables would look like separately if that can help:
Usernames
------------------------------------------------
| Username | FirstName | LastName | ... |
|-----------------|-----------|----------|-----|
| jdoe@mail.com | John | Doe | ... |
| jsmith@mail.com | James | Smith | ... |
| jjones@mail.com | Jane | Jones | ... |
| ... | ... | ... | ... |
------------------------------------------------
Mass_Mail
----------------------------------------------------
| ID_Mass_Mail | Username | Date | ... |
|--------------|----------------|------------|-----|
| 1 | jdoe@mail.com | 2019-01-16 | ... |
| 2 | jdoe@mail.com | 2019-01-29 | ... |
| 3 | jjones@mail.com| 2019-02-14 | ... |
| ... | ... | ... | ... |
----------------------------------------------------
Sent_Mail_Contacts
---------------------------------------------------------------------
| ID_Mass_Mail | Username | Contact_ID | Contact_Email | ... |
|--------------|----------------|------------|----------------|------
| 1 | jdoe@mail.com | 1 | bob@mail.com | ... |
| 1 | jdoe@mail.com | 2 | jim@mail.com | ... |
| 1 | jdoe@mail.com | 3 | cindy@mail.com | ... |
| ... | ... | ... | ... | ... |
| 2 | jdoe@mail.com | 4 | mike@mail.com | ... |
| 2 | jdoe@mail.com | 2 | jim@mail.com | ... |
| 2 | jdoe@mail.com | 3 | cindy@mail.com | ... |
| ... | ... | ... | ... | ... |
---------------------------------------------------------------------