总结 sql server 2008 中的一些 xml 节点值 [英] sum some xml nodes values in sql server 2008

查看:30
本文介绍了总结 sql server 2008 中的一些 xml 节点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑这个 XML:

<Parent ID="p">
    <Child ID="1">10</Child > 
    <Child ID="2">20</Child > 
    <Child ID="3">0</Child > 
</Parent > 

我想用 ID="p"Parent 节点内的所有子值进行 SUM.对于上面的例子,我希望查询返回 <代码>30

I want to SUM all child value inside the Parent node with ID="p".for above example I want query return 30

我该怎么做?

推荐答案

select @xml.value('sum(/Parent[@ID = "p"]/Child)', 'float') as Sum

float 的使用可以防止没有带有该 IDParent.然后,您可以将此结果转换为 int.

The use of float protects against there being no Parent with that ID. You can then cast this result to int.

这篇关于总结 sql server 2008 中的一些 xml 节点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆