OUTER JOIN result is missing rows, no WHERE clause (Workaround found)

前端 未结 1 1974
我在风中等你
我在风中等你 2021-01-12 18:02

Update at the bottom.

I am trying to do a self outer join that, for each record, returns it and all other records occuring later than it, or NULL if it itself is t

1条回答
  •  萌比男神i
    2021-01-12 18:35

    You are not missing anything. If this happens, it's a bug.

    The engine used by MS-Access has several bugs. I've seen similar, inavlid behaviour in joins that had "complex" ON conditions. See another SO question where Access gives buggy results: Why does my left join in Access have fewer rows than the left table?

    You can try the query with identical data in SQL-Server, Oracle, Postgres, even MySQL and you will get the correct, expected results.


    As a workaround, you can try rewriting the query with a UNION, but one can never be sure about the correctness:

    SELECT A.[CR#], A.REGIS_STATUSDATE, B.REGIS_STATUSDATE
    FROM CR_ADMIN_REGIS_STATUS A 
      INNER JOIN CR_ADMIN_REGIS_STATUS B
        ON  A.[CR#]=B.[CR#] 
        AND A.REGIS_STATUSDATE < B.REGIS_STATUSDATE
    
    UNION ALL
    
    SELECT A.[CR#], A.REGIS_STATUSDATE, NULL
    FROM CR_ADMIN_REGIS_STATUS A 
    WHERE NOT EXISTS
          ( SELECT *
            FROM CR_ADMIN_REGIS_STATUS B
            WHERE A.[CR#]=B.[CR#] 
              AND A.REGIS_STATUSDATE < B.REGIS_STATUSDATE
          ) ;
    

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