gpt4 book ai didi

xquery - 使用 XQuery 求和

转载 作者:行者123 更新时间:2023-12-02 09:28:06 28 4
gpt4 key购买 nike

我正在使用 XQuery 执行加法。以下是数据库中保存的XML结构:

    <Events>
<Event>
<id>1</id>
<code>1001</code>
<Amount>50,1</Amount>
</Event>
<Event>
<id>1</id>
<code>1002</code>
<Amount>5,5</Amount>
</Event>
<Event>
<id>1</id>
<code>1001</code>
<Amount>50,1</Amount>
</Event>
<Event>
<id>1</id>
<code>1002</code>
<Amount>5,5</Amount>
</Event>
</Events>

我想通过使用 XQuery 获得以下输出:具有相同代码的金额总和。请注意,.。我需要将 , 替换为 . 并执行算术运算。

 <Total>               
<1001> 100,2 </1001>
<1002> 11,0 </1002>
</Total>

最佳答案

如果您的 XQuery 处理器支持 XQuery 3.0,请使用 group by声明。

<Total>
{
for $i in //Event
let $code := $i/code
group by $code
return element {"code"} { attribute {"id"} {$code}, sum($i/Amount)}
}
</Total>

您的问题中的 XML 片段有两个区别:我将浮点分隔符更改为点(这是必需的,当然您也可以使用一些 XQuery 字符串操作来完成此操作)并且元素名称可能不包含数字只是,看看element naming rules 。我决定在示例中将代码作为 id-attribute 返回。

关于xquery - 使用 XQuery 求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10650655/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com