Oracle XMLTYPE 根据条件从节点中提取第一行值

Oracle XMLTYPE extract first row value from node on condition

SELECT * FROM v$version;
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
PL/SQL Release 12.1.0.2.0 - Production
"CORE   12.1.0.2.0  Production"
TNS for Linux: Version 12.1.0.2.0 - Production
NLSRTL Version 12.1.0.2.0 - Production

这是我以前的 的增强版本,我有 XML 的示例查询,如下所示:

with t(xml) as 
(
select xmltype(
'<SSO_XML
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
TimeStamp="2020-08-05T21:57:23Z" 
Target="Production" 
Version="1.0" 
TransactionIdentifier="PLAN_A" 
SequenceNmbr="123456"
    xmlns="http://www.w3.org/2001/XMLSchema">
    <PlanCode PlanCodeCode="CHOICE">
        <S_DAYS PCODE="P123">
            <STUDENT>
                <DIVISION Amount="100.05" Code="Flat" S_CODE="1" />
                <DIVISION Amount="200.05" Code="Flat" S_CODE="2" />
            </STUDENT>
        </S_DAYS>
        <S_DAYS PCODE="P1234">
            <STUDENT>
                <DIVISION Amount="300.05" Code="Flat" S_CODE="100" />
                <DIVISION Amount="400.05" Code="Flat" S_CODE="110" />
                <DIVISION Amount="500.05" Code="Flat" S_CODE="1" />
                <DIVISION Amount="600.05" Code="Flat" S_CODE="20" />
            </STUDENT>
        </S_DAYS>
          <S_DAYS PCODE="Child1">
            <AdditonalFare>
              <AdditonalFareAmount Amount="100"/>
            </AdditonalFare>
          </S_DAYS> 
          <S_DAYS PCODE="Child2">
            <AdditonalFare>
              <AdditonalFareAmount Amount="130"/>
            </AdditonalFare>
          </S_DAYS> 
    </PlanCode>
</SSO_XML>') 
 from dual
 )
 select h.PlanCodeCode, b.amount, b.S_CODE, h.child1_amount, h.child2_amount
 , s_code_one_amount
 --, FIRST_ROW_AMOUNT
 from   t
    cross apply
    xmltable(xmlnamespaces(default 'http://www.w3.org/2001/XMLSchema'),
             '/SSO_XML'
             passing t.xml
             columns PlanCodeCode varchar2(100)  path './PlanCode/@PlanCodeCode',
                     attributes xmltype path './PlanCode',
                     child1_amount number path './PlanCode/S_DAYS[@PCODE="Child1"]/AdditonalFare/AdditonalFareAmount/@Amount',
                     child2_amount number path './PlanCode/S_DAYS[@PCODE="Child2"]/AdditonalFare/AdditonalFareAmount/@Amount'
            ) h
    outer apply xmltable(xmlnamespaces(default 'http://www.w3.org/2001/XMLSchema'),
             'PlanCode/S_DAYS/STUDENT/DIVISION'
             passing h.attributes
             columns node_level for ordinality
                    , amount number path '@Amount'
                    , S_CODE  varchar2(10) path '@S_CODE'
                    , s_code_one_amount number path '/DIVISION[S_CODE="1"]/@Amount'
                    --, first_row_amount for ordinality/[@Amount]   exception ORA-00904: : invalid identifier                   
            ) b;

我试图在一列中获取第一个节点行,并在另一列中根据 S_CODE = "1" 的条件获取值。但这是我的实际和预期结果:

实际结果:

预期结果:

任何帮助将不胜感激。谢谢。

编辑 添加新的预期结果:

两个最明显的解决方案似乎是...

  1. 在路径中使用 xquery 'if' 有条件地 return 金额,例如
WITH   ...
SELECT ...
FROM   ...
       XMLTABLE (
          xmlnamespaces (DEFAULT 'http://www.w3.org/2001/XMLSchema'),
          'PlanCode/S_DAYS/STUDENT/DIVISION'
          PASSING h.attributes
          COLUMNS
             amount NUMBER PATH '@Amount',
             s_code VARCHAR2 (10) PATH '@S_CODE',
             s_code_one_amount NUMBER
                PATH 'if (@S_CODE="1") then @Amount else null',
             first_row_amount NUMBER
                PATH 'if (count(preceding-sibling::*) = 0) then @Amount else null') b;
  1. 使用 xquery 到 return 节点位置并处理 SQL 中的余数,例如
WITH   ...
SELECT ...
       CASE
          WHEN b.s_code = '1' THEN
             b.amount
       END
          s_code_one_amount,
       CASE
          WHEN b.position = 0 THEN
             b.amount
       END
          first_row_amount
FROM   ...
       OUTER APPLY
       XMLTABLE (
          xmlnamespaces (DEFAULT 'http://www.w3.org/2001/XMLSchema'),
          'PlanCode/S_DAYS/STUDENT/DIVISION'
          PASSING h.attributes
          COLUMNS
             position NUMBER PATH 'count(preceding-sibling::*)',
             amount NUMBER PATH '@Amount',
             s_code VARCHAR2 (10) PATH '@S_CODE') b;