多个嵌套内部联接:并非显示所有记录

时间:2019-03-06 14:17:02

标签: sql-server

我很难连接两个如下表:

  • 主表PMEOBJECT具有一个名为OBJECTID的唯一键,并且 总共有12768行。
  • 然后,我要加入与PMEOBJECT具有n:1关系的PMEOBJECTVALIDITY,因为它有更多的行, 因为它可以保存PMEOBJECT随时间的变化(即当某个特定对象不在 仍然有效),该行有12789行(意味着只有21个对象) 随时间变化)。但是,我只想拥有最新的 VALIDFROM日期显示在查询中。这一切都很好。
  • 然后,当我想加入PMEOBJECTDIMENSION时,麻烦就开始了。 与PMEOBJECTVALIDITY的关系为n:1,共有36737行。

            SELECT 
                PMEOBJECT.OBJECTID
                ,PMEOBJECTVALIDITY.VALIDFROM
                ,PMEOBJECTDIMENSION.DIMENSION2_
            FROM PMEOBJECT
                    LEFT JOIN PMEOBJECTVALIDITY
                        ON PMEOBJECTVALIDITY.OBJECTID = PMEOBJECT.OBJECTID
                        AND PMEOBJECTVALIDITY.DATAAREAID = PMEOBJECT.DATAAREAID
                        INNER JOIN(
                            SELECT 
                                OBJECTID,
                                MAX(VALIDFROM) AS NEWFROMDATE,
                                MAX(VALIDTO) AS NEWTODATE
                            FROM PMEOBJECTVALIDITY B
                            GROUP BY OBJECTID
                            ) B 
                            ON PMEOBJECTVALIDITY.OBJECTID = B.OBJECTID 
                            AND PMEOBJECTVALIDITY.VALIDFROM = B.NEWFROMDATE
                    LEFT JOIN PMEOBJECTDIMENSION
                        ON PMEOBJECTDIMENSION.OBJECTVALIDITYID = PMEOBJECTVALIDITY.RECID
                        AND PMEOBJECTDIMENSION.DATAAREAID = PMEOBJECTVALIDITY.DATAAREAID
                        INNER JOIN(
                            SELECT 
                                OBJECTVALIDITYID,
                                MAX(VALIDFROM) AS NEWFROMDATE_2
                            FROM PMEOBJECTDIMENSION C
                            GROUP BY OBJECTVALIDITYID
                            ) C
                            ON PMEOBJECTDIMENSION.OBJECTVALIDITYID = C.OBJECTVALIDITYID 
                            AND PMEOBJECTDIMENSION.VALIDFROM = C.NEWFROMDATE_2
    

每步查询结果:

  • 选择PMEOBJECT:12768行
  • 左联接PMEVALIDITY:12789行
  • 内部联接的可用性:12768行
  • 左联接PMEOBJECTDIMENSION:36737行
  • 内部联接目标尺寸:12729行

我希望最终结果再次具有相同的12768行,我不想遗漏任何ObjectId。

我在这里想念什么?

亲切的问候, 伊戈尔(Igor)

2 个答案:

答案 0 :(得分:1)

以下内容可能会有所帮助:

从PMEOBJECTDIMENSION开始:

Select * from dba_users where oracle_maintained = 'N';

,如果存在重复,则选择不同的记录。

答案 1 :(得分:1)

INNER JOIN正在过滤记录-您想要的是LEFT JOIN表(PMEOBJECTVALIDITYPMEOBJECTDIMENSION)应仅包含至少具有匹配项的记录INNER JOIN查询中的别名(别名BC)。您可以通过将INNER JOINLEFT JOIN嵌套来完成此操作,通常按以下步骤操作:

SELECT *
FROM A
LEFT JOIN B
    INNER JOIN C
    ON B.ID = C.BID
ON A.ID = B.AID

现在BINNER JOINC,将仅包含在C中具有匹配项的记录,但将保留LEFT JOIN而不会删除任何记录A中的记录。

根据您的情况,您可以简单地将ON子句从LEFT JOIN移动到下一个INNER JOIN的末尾。

    SELECT 
        PMEOBJECT.OBJECTID
        ,PMEOBJECTVALIDITY.VALIDFROM
        ,PMEOBJECTDIMENSION.DIMENSION2_
    FROM PMEOBJECT
            LEFT JOIN PMEOBJECTVALIDITY
                INNER JOIN(
                    SELECT 
                        OBJECTID,
                        MAX(VALIDFROM) AS NEWFROMDATE,
                        MAX(VALIDTO) AS NEWTODATE
                    FROM PMEOBJECTVALIDITY B
                    GROUP BY OBJECTID
                    ) B 
                    ON PMEOBJECTVALIDITY.OBJECTID = B.OBJECTID 
                    AND PMEOBJECTVALIDITY.VALIDFROM = B.NEWFROMDATE
            ON PMEOBJECTVALIDITY.OBJECTID = PMEOBJECT.OBJECTID
                AND PMEOBJECTVALIDITY.DATAAREAID = PMEOBJECT.DATAAREAID --here it is!
            LEFT JOIN PMEOBJECTDIMENSION                    
                INNER JOIN(
                    SELECT 
                        OBJECTVALIDITYID,
                        MAX(VALIDFROM) AS NEWFROMDATE_2
                    FROM PMEOBJECTDIMENSION C
                    GROUP BY OBJECTVALIDITYID
                    ) C
                    ON PMEOBJECTDIMENSION.OBJECTVALIDITYID = C.OBJECTVALIDITYID 
                    AND PMEOBJECTDIMENSION.VALIDFROM = C.NEWFROMDATE_2
           ON PMEOBJECTDIMENSION.OBJECTVALIDITYID = PMEOBJECTVALIDITY.RECID
                AND PMEOBJECTDIMENSION.DATAAREAID = PMEOBJECTVALIDITY.DATAAREAID --I'm here