MySQL count(distinct(email) and Group By DATE(entrydate)
Having a bit of trouble with getting total unique records and grouping by date. The end result is I am getting totals per day but it is not using unique emails based on the distinct function. Here is my query...
SELECT count(distinct(emailaddress)), DATE(EntryDate)
FROM tblentries
group by DATE(EntryDate)
ORDER BY DATE(EntryDate) desc
The results end up not de-dupi开发者_StackOverflow中文版ng the count for each day. Thoughts?
Thanks!
Based on the conversation, I believe what you are looking for is the number of distinct never-before-seen email addresses per day:
SELECT
DATE(t.EntryDate) as RecordDate,
COUNT(DISTINCT t.emailaddress) as NewEmailAddresses
FROM
tblentries t
WHERE
NOT EXISTS(
SELECT 1
FROM tblentries t2
WHERE
t2.emailaddress = t.emailaddress
AND DATE(t2.EntryDate) < DATE(t.EntryDate)
)
GROUP BY
DATE(t.EntryDate)
ORDER BY
DATE(t.EntryDate) ASC;
This is off the top of my head, so it may not be right, and it will be slow, but I think this is in the right direction. On a side note, if you plan on running this regularly, an index on emailaddress would be a good idea.
Let me know if this works.
精彩评论