Access 2007 - Left Join to a query returns #Error instead of Null

前端 未结 3 1458
傲寒
傲寒 2020-12-02 01:34

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 b

相关标签:
3条回答
  • 2020-12-02 01:48

    While the query should return Null based on the join type, as Allen Browne states in his article, Bug: Outer join expressions retrieved wrongly,

    "Instead, it behaves as if [the JET query optimizer] is evaluating the expression after it has returned the results from the lower-level query."

    Consequently, you must select the calculated field using an expression that will evaluate to Null if the right-side of the join doesn't match the left-side.

    Using your pared-down code as an example:

    SELECT 
    Month.Chain,
    DateDiff("m",QueryDates.StartDate,QueryDates.EndDate)+1 AS CasesPerMonthPerStore
    FROM
    QueryDates,
    MonthRange;
    
    SELECT
    Chains.Chain,
    IIf(IsNull(ErrorQuery.Chain),Null,ErrorQuery.CasesPerMonthPerStore)
    FROM
    Chains
    LEFT JOIN
    ErrorQuery
    ON Chains.Chain=ErrorQuery.Chain;
    
    0 讨论(0)
  • 2020-12-02 01:55

    This is old, but I found a work around that I hope helps somebody. I had the same issue with a right-side query, but I also had a parallel right-side query with a similar data source and derivation that was working correctly in a left join. The difference was there was a UNION query between the working right-side and an underlying query with a simple calculated field. So I put a UNION query between the troublesome right-side query and the final query by creating an empty table with the same fields creating a union with that table and the original right-side query. Worked well. Based on the information from Wilskt and Allen Browne, I think that the UNION is forcing Jet to delay evaluation.

    0 讨论(0)
  • 2020-12-02 02:02

    It's looking like it could be to do with a known bug in Access, whereby it makes mistakes on outer joins with calculated fields:

    http://allenbrowne.com/BugOuterJoinExpression.html

    and

    http://allenbrowne.com/bug-10.html

    So going to see if I can rejig the subqueries to disguise the calculated fields somehow.

    I love Access. :)

    0 讨论(0)
提交回复
热议问题