在Oracle SQL中需要有关XMLAgg的帮助

时间:2019-01-18 19:50:48

标签: sql oracledb

我正在使用Oracle数据库中的XMLAgg获取所需的XML结构。

数据格式为:表/视图名称为TA_LN_ITEM_COMPLIANCE_V

PROD_ID         COO Multiple_Coo    Country_code    HS  ECN
FT34204-10000AB null    US ??                 US    846711  XYZ
FT34204-10000AB null    US ??                 BE    8467119 ABC
FT34204-10000AB null    US ??                 CZ    8467119 CDG
FT34204-10000AB null    US ??                  1    8467119 NOCLASS
FT34204-10000AB null    US ??                 AU    8467119 NOCLASS

Please see attached image for data format

select 
      xmlagg(xmlelement("DataArea",
      xmlelement("Item",
      xmlforest(
                a.prod_id AS "TA_ITEM",
                NVL(TO_CHAR(a.CTRY_OF_ORIGIN),' ') AS "CTRY_OF_ORIGIN",
                NVL(TO_CHAR(a.MULTIPLE_COO),' ') AS "MULTIPLE_COO"),
                ( select xmlagg(xmlelement("Item_Compliance",
                 xmlforest(
                           b.CTRY_CODE AS "CTRY_CODE",
                           b.EX_HS AS "EX_HS",
                           b.ECCNUM AS "ECCNUM"))) from TA_LN_ITEM_COMPLIANCE_V b where a.prod_id=b.prod_id)"Item_Compliance" ))).getClobVal() AS xml_frag
                           from TA_LN_ITEM_COMPLIANCE_V a,TA_LN_ITEM_COMPLIANCE_V b  where a.prod_id=b.prod_id

期望是这样的

<DataArea>
    <Item>
        <TA_ITEM>FT34204-10000AB</TA_ITEM>
        <CTRY_OF_ORIGIN>
        </CTRY_OF_ORIGIN>
        <MULTIPLE_COO>US ??</MULTIPLE_COO>
        <Item_Compliance>
            <CTRY_CODE>US</CTRY_CODE>
            <EX_HS>8467115040</EX_HS>
            <ECCNUM>XYZ</ECCNUM>
        </Item_Compliance>
        <Item_Compliance>
            <CTRY_CODE>BE</CTRY_CODE>
            <EX_HS>8467119</EX_HS>
            <ECCNUM>ABC</ECCNUM>
        </Item_Compliance>
        <Item_Compliance>
            <CTRY_CODE>CZ</CTRY_CODE>
            <EX_HS>8467119</EX_HS>
            <ECCNUM>CDG</ECCNUM>
        </Item_Compliance>
        <Item_Compliance>
            <CTRY_CODE>01</CTRY_CODE>
            <EX_HS>8467119</EX_HS>
            <ECCNUM>NOCLASS</ECCNUM>
        </Item_Compliance>
        <Item_Compliance>
            <CTRY_CODE>AU</CTRY_CODE>
            <EX_HS>8467119</EX_HS>
            <ECCNUM>NOCLASS</ECCNUM>
        </Item_Compliance>
    </Item>
</DataArea>

0 个答案:

没有答案