从 Oracle 中的左连接子查询中检索列

Retrieving a column from a left join subquery in Oracle

我在 spl 子查询中添加左外连接 EDW_SPLITTER_NT_PORT 后出现错误 [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 彼此相关,因此我将左连接添加到 table。有什么建议吗?

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

如果我没看错你的问题,你是说 PORT_AID 列来自 n 子查询。但是,您试图在 spl 子查询内使用它,这超出了 n 子查询的范围。您必须在 spl 子查询中添加更多连接才能在子查询中公开该列,或者您可以保留它并在外部查询中引用它。

此外,您在外部 select 中使用的 splitter 别名无效。

您的子查询 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 但此子查询中没有派生 table n。当你在这里说 n.PORT_AID 时,它不知道你在说什么。