Hi, I have a feeling I am making some sort of foolish mistake here, however I am trying to do a query over two tables. One table contains the value I want to aggregate over, in this case I have called if the StoreCharge table. The other table contains the values I want to count.
SELECT StoreCharge.StoreId,
COUNT(DISTINCT(ISNULL(WholesalerInvoice.WholesalerId,0))) AS Invoices
FROM StoreCharge
LEFT OUTER JOIN
WholesalerInvoice ON StoreCharge.StoreId = WholesalerInvoice.StoreId
WHERE StoreCharge.CompanyId = 2
AND WholesalerInvoice.StoreInvoiceId IS NULL
AND DATEDIFF(day,WholesalerInvoice.InvoiceDate,'20100627') > =0
AND DATEDIFF(day,dateadd(day,-7,'20100627'),WholesalerInvoice.InvoiceDate) > 0
GROUP BY StoreCharge.StoreId
My problem is that if there are rows in the counting table that match the WHERE clause, the query works ok. However When no rows match the criteria nothing is returned instead of a list of the values in StoreCharge with a count of 0.