【问题标题】:Retrieving a column from a left join subquery in Oracle从 Oracle 中的左连接子查询中检索列
【发布时间】:2015-05-20 06:27:33
【问题描述】:

在我将左外连接 EDW_SPLITTER_NT_PORT 添加到我的 spl 子查询后,我收到错误 [Error] Execution (12: 25): ORA-00904: "N"."PORT_AID": invalid identifier。我怎样才能让我的查询工作?它从n 子查询中获取N.PORT_AID 并且它没有在spl 中到达它?我是否应该在spl 子查询中更改我的左连接EDW_SPLITTER_NT_PORT 并将其连接到底部的n 子查询。

EDW_SPLITTEREDW_SPLITTER_NT_PORT 彼此相关,这就是我将左连接添加到该表的原因。有什么建议吗?

Select spl.splitter_addr AS splitter_address,
          spl.splitter_port,
          spl_port_status
          from (
   SELECT splitter.splitter_addr AS splitter_address,
          splitter.splitter_port,
          CASE
             WHEN splitter.SPLITTER_PORT_STATUS = 'ACTIVE'
             THEN
                CASE
                   WHEN n.PORT_AID IS NULL THEN 'INACTIVE'
                   ELSE 'ACTIVE'
                END
             ELSE --follow through with whatever splitter_port_status other than active remains:
                DECODE (splitter.splitter_port_status,
                        'IN-ACTIVE', 'INACTIVE',
                        splitter.splitter_port_status)
          END
             AS splitter_port_status

     FROM ls2.edw_splitter@WBCMLS1P.sbc.com splitter 
                          left outer join EDW_SPLITTER_NT_PORT nt
    --  ON     splitter.splitter_addr = nt.splitter_addr
    ON     splitter.eid = nt.eid
                          AND splitter.rack = nt.rack
                          AND splitter.shelf = nt.shelf
                          AND splitter.card = nt.card
                          AND splitter.port = nt.port)  spl


          LEFT OUTER JOIN
          (SELECT a.eid,
                  a.location_id,
                  o.rack,
                  o.shelf,
                  TO_NUMBER (SUBSTR (card, -2, 2)) AS slot,
                  o.port,
                  o.ont,
                  o.port_aid,
                  o.ont_type
             FROM ls2.actl73x0@WBCMLS1P.sbc.com a
                  LEFT OUTER JOIN ls2.ACTL73XX_ONT@WBCMLS1P.sbc.com o
                     ON a.eid = o.eid
           UNION ALL
           SELECT b.eid,
                  b.location_id,
                  1 rack,
                  1 shelf,
                  bo.card AS slot,
                  bo.port,
                  bo.ont,
                  REPLACE (bo.ont_aid, 'ONT', 'ONT-1-1') AS port_aid,
                  bo.ont_type
             FROM ls2.blm@WBCMLS1P.sbc.com b
                  LEFT OUTER JOIN ls2.blm_ont@WBCMLS1P.sbc.com bo
                     ON b.eid = bo.eid) n
             ON     spl.eid = n.eid
                AND n.rack = spl.rack
                AND n.shelf = spl.shelf
                AND n.slot = spl.slot
                AND n.port = spl.port
                AND n.ont = spl.ont

【问题讨论】:

    标签: sql oracle left-join


    【解决方案1】:

    如果我正确阅读了您的问题,您的意思是 PORT_AID 列来自 n 子查询。但是,您尝试在 spl 子查询中使用它,这超出了 n 子查询的范围。您必须在 spl 子查询中添加更多连接才能在子查询中公开该列,或者您可以保留它并在外部查询中引用它。

    另外,您在外部选择中使用的 splitter 别名无效。

    【讨论】:

    • 是的,你没看错,这就是我想要做的,我需要 spl 子查询中的 port_AID 列。是的,你的权利外部选择需要有spl 引用而不是splitter,但这是我最不关心的。
    • 所以您需要做的就是将 case 语句从 spl 子查询移到外部查询。您必须确保所有相关列都在连接子查询的选择列表中。
    【解决方案2】:

    你的子查询spl:

    SELECT splitter.splitter_addr AS splitter_address,
        splitter.splitter_port,
        CASE 
            WHEN splitter.SPLITTER_PORT_STATUS = 'ACTIVE'
                THEN CASE 
                        WHEN n.PORT_AID IS NULL
                            THEN 'INACTIVE'
                        ELSE 'ACTIVE'
                        END
            ELSE --follow through with whatever splitter_port_status other than active remains:
                DECODE(splitter.splitter_port_status, 'IN-ACTIVE', 'INACTIVE', splitter.splitter_port_status)
            END AS splitter_port_status
    FROM ls2.edw_splitter@WBCMLS1P.sbc.com splitter
    LEFT JOIN EDW_SPLITTER_NT_PORT nt
        --  ON     splitter.splitter_addr = nt.splitter_addr
        ON splitter.eid = nt.eid
            AND splitter.rack = nt.rack
            AND splitter.shelf = nt.shelf
            AND splitter.card = nt.card
            AND splitter.port = nt.port
    

    引用n.PORT_AID,但此子查询中没有派生表n。当你在这里说n.PORT_AID 时,它不知道你在说什么。

    【讨论】:

    • 是的n.port_aid 超出了spl 的范围,只是想知道如何才能使其在范围内,我应该删除 spl 括号和外部选择,然后做一个左外部连接,然后是另一个左外连接
    猜你喜欢
    • 2016-01-28
    • 2018-11-01
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 2012-08-31
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多