Thanks Dagon, here is what I'm working with, simplified versions.
Dates table / sample data:
CREATE TABLE IF NOT EXISTS `dates` (
`datevalue` int(8) NOT NULL,
`date` date NOT NULL,
PRIMARY KEY (`datevalue`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `dates`
--
INSERT INTO `dates` (`datevalue`, `date`) VALUES
(20101201, '2010-12-01'),
(20101202, '2010-12-02'),
(20101203, '2010-12-03'),
(20101204, '2010-12-04'),
(20101205, '2010-12-05'),
(20101206, '2010-12-06'),
(20101207, '2010-12-07'),
(20101208, '2010-12-08');
Sales table / sample data
CREATE TABLE IF NOT EXISTS `brand_sales` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` date NOT NULL,
`brand_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
--
-- Dumping data for table `brand_sales`
--
INSERT INTO `brand_sales` (`id`, `date`, `brand_id`) VALUES
(1, '2010-12-01', 1),
(2, '2010-12-01', 2),
(3, '2010-12-03', 2),
(4, '2010-12-04', 1);
Here is the version of the query that gets me all dates with holes filled in, this does not limit which brand I'm pulling counts.
SELECT dates.date, brand_id, count(id) as sales FROM dates
LEFT JOIN brand_sales ON dates.date = brand_sales.date
WHERE dates.date >= '2010-12-01' AND dates.date <= '2010-12-08'
GROUP BY brand_id, dates.date
This is close to what I want. Days that do not have sales at all show up as null. I want days that do not have sales for the given brand to show up as null as well.
If I limit my results based on the brand_id, I'm back to having just days with sales:
SELECT dates.date, brand_id, count(id) FROM dates
LEFT JOIN brand_sales ON dates.date = brand_sales.date
WHERE dates.date >= '2010-12-01' AND dates.date <= '2010-12-08'
GROUP BY brand_id, dates.date HAVING brand_id = 1
If I put an OR brand_id is null, I get dates that have sales for my brand plus dates with no sales at all. Because brand 2 had sales on the days that brand 1 doesn't, the date is omitted to match my where clause.
SELECT dates.date, brand_id, count(id) FROM dates
LEFT JOIN brand_sales ON dates.date = brand_sales.date
WHERE dates.date >= '2010-12-01' AND dates.date <= '2010-12-08'
GROUP BY brand_id, dates.date HAVING (brand_id = 1 OR brand_id is null)
Let me know if there is anything else I can do to help you help me, your time is very much appreciated.