Similar to this question, but their problem with the query was never fully solved:
#Error showing up in multiple LEFT JOIN statement Access query when value should be NULL
I get #Error when I'm expecting to see a Null when doing a left join where there is no corresponding record on the right hand side of the join:
Chain CasesPerMonthPerStore MonthOfFirstOrder
Naturally 2.3 5/1/2011
Tom's Market #Error
Livingstons #Error
EverClear 3.1 7/1/2012
Bob's Market 2.66 5/1/2012
Andy's Exports #Error
Jamestowns 0.89 7/1/2012
It works fine if I copy the data into a table and left join to this table, so I assume there is something wrong in the syntax of the query:
SELECT
MonthRange.Chain,
MonthRange.CasesShipped/IIf(MonthsSinceFirstOrder.Months>DateDiff("m",QueryDates.StartDate,QueryDates.EndDate)+1,
DateDiff("m",QueryDates.StartDate,QueryDates.EndDate)+1,
MonthsSinceFirstOrder.Months)/NumStores.NumberOfStores AS CasesPerMonthPerStore,
MonthsSinceFirstOrder.MonthOfFirstOrder
FROM
QueryDates,
(
MonthRange
INNER JOIN
NumStores
ON MonthRange.Chain=NumStores.Chain
)
INNER JOIN
MonthsSinceFirstOrder
ON MonthRange.Chain=MonthsSinceFirstOrder.Chain;
This SQL returns the correct results, it's just its behaviour when Left Joining to it that is returning #Errors.
nb the strange Iif statement in the middle checks to see if the number of months since the first order is greater than the number of months included in the specified date range - so if the date range has 6 months and the first order was 9 months before the end date, it uses 6; if the first order was only 4 months before the end date, it uses 4.
-- EDIT UPDATE --
Right, I took elements of the query out one by one, and this is the simplest I can get whilst still recreating the left joining error:
SELECT
MonthRange.Chain,
DateDiff("m",QueryDates.StartDate,QueryDates.EndDate)+1 AS CasesPerMonthPerStore
FROM
QueryDates,
MonthRange;
And this is how I'm left joining to it:
SELECT
Chains.Chain,
ErrorQuery.CasesPerMonthPerStore
FROM
Chains
LEFT JOIN
ErrorQuery
ON Chains.Chain=ErrorQuery.Chain;
Does anything in this SQL look wrong?