如何从oracle DB中的多标记xml中获取提取值的SUM

时间:2015-06-16 17:46:07

标签: sql xml oracle aggregate-functions

我可以使用你的一点帮助。我在oracle DB中有一个表“card_activity”

列:

INTERVENTION_ID NUMBER(13,0)
CARD_NUM    NUMBER(19,0)
ACCOUNT_ID  NUMBER(19,0)
REQUESTED_AMT   NUMBER(8,2)
ACTUAL_AMT  NUMBER(8,2)
AUTHORIZED_AMT  NUMBER(8,2)
SITE_CD VARCHAR2(25 BYTE)
EXTERNAL_TRACE_ID   VARCHAR2(12 BYTE)
EXTERNAL_BATCH_ID   VARCHAR2(3 BYTE)
EXTERNAL_TICKET_ID  VARCHAR2(4 BYTE)
EXTERNAL_TERMINAL_ID    VARCHAR2(25 BYTE)
EXTERNAL_ACTIVITY_DT    DATE
PAIRED_INTERVENTION_ID  NUMBER(13,0)
REVERSAL_FLAG   VARCHAR2(1 BYTE)
RECONCILE_INTERVENTION_ID   NUMBER(13,0)
SALES_ORDER_INTERVENTION_ID NUMBER(13,0)
INVENTORY_INTERVENTION_ID   NUMBER(13,0)
LAST_INTERVENTION_ID    NUMBER(13,0)
EXT_XML CLOB

数据:

73141   7042310000000002253 36887   -20     -17.75  00303563    000001  001 0001    PLD0        31-AUG-2012 02.23.52        N                   <preRedeem><cplValue>5</cplValue><cplQuantity>355.0</cplQuantity><cplRequestedAmount>17.750</cplRequestedAmount><cplAuthorizedAmount>17.750</cplAuthorizedAmount><washValue>0</washValue><washRequestedAmount>10.00</washRequestedAmount><washAuthorizedAmount>0</washAuthorizedAmount><products><product><productCode>03</productCode><productType>F</productType><quantity>200.0</quantity><salesAmount>50.00</salesAmount><taxInclusiveFlag>I</taxInclusiveFlag><discountAmount>0.00</discountAmount><provinceCode>ON</provinceCode></product><product><productCode>W3</productCode><productType>C</productType><quantity>0.0</quantity><salesAmount>10.00</salesAmount><taxInclusiveFlag>E</taxInclusiveFlag><discountAmount>0.00</discountAmount><provinceCode>ON</provinceCode></product><product><productCode>05</productCode><productType>F</productType><quantity>155.0</quantity><salesAmount>25.50</salesAmount><taxInclusiveFlag>I</taxInclusiveFlag><discountAmount>0.00</discountAmount><provinceCode>ON</provinceCode></product></products></preRedeem>

73225   7042310000000002287 37541   -5      -0.2    00302289    015730  333 0853    LBR0        04-SEP-2012 10.01.50        N                   <preRedeem><cplValue>5</cplValue><cplMaxLitre>0</cplMaxLitre><cplQuantity>3.9</cplQuantity><cplRequestedAmount>0.195</cplRequestedAmount><cplAuthorizedAmount>0.195</cplAuthorizedAmount><washValue>0</washValue><washRequestedAmount>0</washRequestedAmount><washAuthorizedAmount>0</washAuthorizedAmount><products><product><productCode>20</productCode><productType>F</productType><quantity>3.9</quantity><salesAmount>5.00</salesAmount><taxInclusiveFlag>I</taxInclusiveFlag><discountAmount>0.00</discountAmount><provinceCode>ON</provinceCode></product></products></preRedeem>

分解xml 第1行:

<preRedeem>
    <cplValue>5</cplValue>
    <cplQuantity>355.0</cplQuantity>
    <cplRequestedAmount>17.750</cplRequestedAmount>
    <cplAuthorizedAmount>17.750</cplAuthorizedAmount>
    <washValue>0</washValue>
    <washRequestedAmount>10.00</washRequestedAmount>
    <washAuthorizedAmount>0</washAuthorizedAmount>
    <products>
        <product>
            <productCode>03</productCode>
            <productType>F</productType>
            <quantity>200.0</quantity>
            <salesAmount>50.00</salesAmount>
            <taxInclusiveFlag>I</taxInclusiveFlag>
            <discountAmount>0.00</discountAmount>
            <provinceCode>ON</provinceCode>
        </product>
        <product>
            <productCode>W3</productCode>
            <productType>C</productType>
            <quantity>0.0</quantity>
            <salesAmount>10.00</salesAmount>
            <taxInclusiveFlag>E</taxInclusiveFlag>
            <discountAmount>0.00</discountAmount>
            <provinceCode>ON</provinceCode>
        </product>
        <product>
            <productCode>05</productCode>
            <productType>F</productType>
            <quantity>155.0</quantity>
            <salesAmount>25.50</salesAmount>
            <taxInclusiveFlag>I</taxInclusiveFlag>
            <discountAmount>0.00</discountAmount>
            <provinceCode>ON</provinceCode>
        </product>
    </products>
</preRedeem>

第二行:

    <preRedeem>
    <cplValue>5</cplValue>
    <cplMaxLitre>0</cplMaxLitre>
    <cplQuantity>3.9</cplQuantity>
    <cplRequestedAmount>0.195</cplRequestedAmount>
    <cplAuthorizedAmount>0.195</cplAuthorizedAmount>
    <washValue>0</washValue>
    <washRequestedAmount>0</washRequestedAmount>
    <washAuthorizedAmount>0</washAuthorizedAmount>
    <products>
        <product>
            <productCode>20</productCode>
            <productType>F</productType>
            <quantity>3.9</quantity>
            <salesAmount>5.00</salesAmount>
            <taxInclusiveFlag>I</taxInclusiveFlag>
            <discountAmount>0.00</discountAmount>
            <provinceCode>ON</provinceCode>
        </product>
    </products>
</preRedeem>

如果在单行的EXT_XML属性下存在多个(“salesAmount”)标记,我需要编写一个查询来提取“salesAmount”作为总和。我尝试了以下查询只是为了解压SalesAmount(没有聚合)。

Select 
extract(xmltype.createxml(ext_xml), '/preRedeem/products/product/salesAmount/text()').getStringVal()
from card_activity

输出:

50.0010.0025.50
50.00

如何修改我的SQL以获取

期望输出:

85.50
50.0

1 个答案:

答案 0 :(得分:0)

这是一个开始的例子。我假设你在一个XML记录中有多个preRedeem元素?那么有一些独特的ID吗?您需要更改查询以同时提取相关的密钥ID和组。如果您编辑问题以显示详细信息,我会尝试更新我的答案。

with card_activity as
(
select xmltype('<preRedeem>
    <cplValue>5</cplValue>
    <cplQuantity>355.0</cplQuantity>
    <cplRequestedAmount>17.750</cplRequestedAmount>
    <cplAuthorizedAmount>17.750</cplAuthorizedAmount>
    <washValue>0</washValue>
    <washRequestedAmount>10.00</washRequestedAmount>
    <washAuthorizedAmount>0</washAuthorizedAmount>
    <products>
        <product>
            <productCode>03</productCode>
            <productType>F</productType>
            <quantity>200.0</quantity>
            <salesAmount>50.00</salesAmount>
            <taxInclusiveFlag>I</taxInclusiveFlag>
            <discountAmount>0.00</discountAmount>
            <provinceCode>ON</provinceCode>
        </product>
        <product>
            <productCode>W3</productCode>
            <productType>C</productType>
            <quantity>0.0</quantity>
            <salesAmount>10.00</salesAmount>
            <taxInclusiveFlag>E</taxInclusiveFlag>
            <discountAmount>0.00</discountAmount>
            <provinceCode>ON</provinceCode>
        </product>
        <product>
            <productCode>05</productCode>
            <productType>F</productType>
            <quantity>155.0</quantity>
            <salesAmount>25.50</salesAmount>
            <taxInclusiveFlag>I</taxInclusiveFlag>
            <discountAmount>0.00</discountAmount>
            <provinceCode>ON</provinceCode>
        </product>
    </products>
</preRedeem>') ext_xml from dual )
select  sum(x.salesamount)
from  card_activity, xmltable('/preRedeem/products/product' passing card_activity.ext_xml
     columns salesAmount number(10,2) path 'salesAmount') x;