拆分数量值每行10行数量值1

时间:2016-10-19 20:34:43

标签: oracle xmltype xmltable

<ROWSET>
 <ROW>
<PONumber>XYZ-3450462</PONumber>
<OfferName>XYZ COMPONENT</OfferName>
<INVLOC>XYZ-ABC2042</INVLOC>
<Quantity>10</Quantity>
</ROW>

每个记录数量值1,表中数量值的总和应与xml数量值相同

输出如下:

---------------------------- 
PONumber OfferName WarehouseLocation Quantity 
XYZ-3450462 XYZ COMPONENT XYZ-ABC2042 1 
XYZ-3450462 XYZ COMPONENT XYZ-ABC2042 1 
XYZ-3450462 XYZ COMPONENT XYZ-ABC2042 1 
XYZ-3450462 XYZ COMPONENT XYZ-ABC2042 1

1 个答案:

答案 0 :(得分:1)

WITH table_name ( xml ) AS (
  SELECT '<ROWSET>
<ROW><PONumber>XYZ-3450462</PONumber><OfferName>XYZ COMPONENT</OfferName>
     <INVLOC>XYZ-ABC2042</INVLOC><Quantity>10</Quantity></ROW>
<ROW><PONumber>ABC-1234567</PONumber><OfferName>ABC COMPONENT</OfferName>
     <INVLOC>ABC-XYZ1234</INVLOC><Quantity>3</Quantity></ROW>
</ROWSET>' FROM DUAL
),
xml_data ( PONumber, OfferName, INVLOC, Quantity, RN ) AS (
  SELECT  PONumber, OfferName, INVLOC, 1, Quantity
  FROM    table_name t,
          XMLTable(
            '/ROWSET/ROW'
            PASSING XMLType( t.xml )
            COLUMNS PONumber  VARCHAR2(20) PATH '/ROW/PONumber',
                    OfferName VARCHAR2(20) PATH '/ROW/OfferName',
                    INVLOC    VARCHAR2(20) PATH '/ROW/INVLOC',
                    Quantity  NUMBER(5,0)  PATH '/ROW/Quantity'
         ) x
UNION ALL
  SELECT  PONumber, OfferName, INVLOC, 1, RN - 1
  FROM    xml_data
  WHERE   RN > 1
)
SELECT PONumber, OfferName, INVLOC, Quantity FROM xml_data;