Bleh, two problems in one day.
I have three tables - Emails, Calls, and Clicks. I need to pull a bunch of data from Clicks within a given date range and add up those columns on a monthly basis, and get the number of entries in Calls and Emails in the same date range (also by month). The dates are stored as YYYY-MM-DD in all 3 tables, not by month, and I need to group them by month. Additionally, if there isn't an entry for that month in Clicks, but there is in Calls or Emails, I need that entry to still be in the final result.
For example, if Clicks doesn't have an entry for 2006-10, but Calls does, the result needs to have a row for 2006-10. The final result would be something like
2006-10 <click data for that month> <# of entries in Emails for that month> <# of entries in Calls for that month>
2006-10 <click data for that month> <# of entries in Emails for that month> <# of entries in Calls for that month>
So here's the basic query from Clicks as it stands right now:
SELECT DATE_FORMAT(cdate, '%Y-%m') Date, source Source, sum(imprss) Impressions, sum(cost) Cost
FROM Clicks
WHERE cdate between '2006-01-01' and '2006-12-31'
GROUP BY MONTH(cdate)
So I guess my first step is to tag this in preparation for a Join.
SELECT DATE_FORMAT(c.cdate, '%Y-%m') Date, c.source Source, sum(c.imprss) Impressions, sum(c.cost) Cost
FROM Clicks c
WHERE cdate between '2006-01-01' and '2006-12-31'
GROUP BY MONTH(cdate)
This is where I start to have trouble. I've never done a 3-way join before, especially not when I'm only trying to get a tally from the other tables instead of a pre-existing data column.