I have an XML field in an SQL table like this
<Root>
<Name>Apollo</Name>
<Code>1000</Code>
<Code>2000</Code>
<Code>3000</Code>
</Root>
I need to write an SQL query to select the values "Name" and SUM "Code"
SELECT
T1.c.value('Name[1] AS VARCHAR(100)') AS Name,
T1.c.value('Code[1] AS NUMERIC(10,5)') AS TotalCode
FROM TableName
CROSS APPLY xmlField.nodes('Root') AS T1(c)
he gives me the output as follows:
Name Code
---------------------------
Apollo 1000
Apollo 2000
Apollo 3000
But I need the SUM values of all Code tags, like this:
Name Code
---------------------------
Apollo 6000
Any ideas on how to get the sum of the tag values?
source
share