[英]How to get the SUM of extracted values from multi-tagged xml in oracle DB
我可以帮您一点帮助。 我在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第一行:
<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
这是一个示例。 我假设您在一个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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.