Eliminate a xml node from sum, in XPath to SQL Server XML column(从 sum 中删除一个 xml 节点,在 XPath 到 SQL Server XML 列中)
问题描述
我有这个 XPath 表达式,它对最深的 XML 层次结构中的所有节点求和,而不使用节点名称:
I have this XPath expression, which sum all nodes in the deepest XML hierarchy, without using nodes names:
select @data.value('sum(//*[not(*)])', 'float')
如何根据名称对一个节点进行例外处理?
How do I make an exception of one node, by its name?
说这是xml:
<c>
<b1>
<a>1</a>
<d>4</d>
<g>5</g>
</b1>
<b1>
<a>7</a>
<d>1</d>
<g>2</g>
</b1>
</c>
我希望总和包含d"和g",没有a",但a"将作为参数传递,因此需要在表达式中表示为参数.我尝试了以下方法:
I would like the sum to contain "d" and "g", without "a", but "a" will be pass as parameter and so need to be represented as parameter inside the expression. I've tried the following:
declare @except varchar(max) = 'a'
select @data.value('sum(//*[not(*)])', 'float') - @data.value('sum(//*:local-name()=sql:variable("@except"))', 'float')
但没有成功.
推荐答案
虽然 Marc 的回答是我最会做的,但我有一个小技巧给你,下面
While Marc's answer is what i would do mostly, I have a small hack for you, below
select @data.value('sum(//*[not(*)])', 'float')- @data.value('sum(//*:a)', 'float')
这篇关于从 sum 中删除一个 xml 节点,在 XPath 到 SQL Server XML 列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 sum 中删除一个 xml 节点,在 XPath 到 SQL Server


基础教程推荐
- 带更新的 sqlite CTE 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01