我有一个SQL查询。我需要总结两个具有相同名称()但不同属性(ident =“1cat”+“3cat”)的xml节点。我可以通过它们的数字([1]和[3])得到节点,但我需要用“ident”来表示它们。我如何用他们的观点来总结1Category和3Category?
DECLARE @xml XML
SET @xml =
'<cat:catalog xmlns:cat="http://datypic.com/cat" xmlns:prod="http://datypic.com/prod">
<cat:number ident="1Category">10</cat:number>
<cat:number ident="2Category">20</cat:number>
<cat:number ident="3Category">30</cat:number>
</cat:catalog>';
WITH XMLNAMESPACES (
'http://datypic.com/cat' AS cat
)
SELECT
c.c.value('(cat:number/text())[1]', 'INT') '1Category',
c.c.value('(cat:number/text())[3]', 'INT') '3Category'
FROM @xml.nodes('cat:catalog') c(c)
答案 0 :(得分:1)
WITH XMLNAMESPACES (
'http://datypic.com/cat' AS cat
)
SELECT
c.c.value('(cat:number[@ident="1Category"])[1]', 'INT') +
c.c.value('(cat:number[@ident="3Category"])[1]', 'INT')
FROM @xml.nodes('cat:catalog') c(c)
答案 1 :(得分:1)
WITH XMLNAMESPACES (
'http://datypic.com/cat' AS cat
)
SELECT @xml.value('sum(/cat:catalog/cat:number[@ident=("1Category", "3Category")]/text())', 'INT')