Paul Mauriello
asked on
How do you write a conditional sum in SQL XML summing one node value based off another?
Im trying to figure out how to write a conditional sum in SQL XML based on another node in this case when type is 'L' then sum only those days.
Does any know the syntax for that?
Does any know the syntax for that?
<root>
<legs>
<item>
<LegDto>
<type>L</type>
<cost>15000</cost>
<days>2</days>
</LegDto>
</item>
<legs>
<root>
SELECT b.VoyageResultID,
b.VoyageID,
c.VoyageObjectXML.value('sum(root/legs/item/LegDto/days)', 'numeric(28,17)') as Portdays, -- The straight sum works fine
c.VoyageObjectXML.value('sum(root/legs/item/LegDto/days)', 'numeric(28,17)') as Portdays, -- SUM only if (root/legs/item/LegDto/type) = 'L'
b.IsActive
FROM [dbo].[tblVoyage] a
JOIN [dbo].[tblVoyageResult] b ON b.VoyageID = a.VoyageID
CROSS APPLY (SELECT CONVERT(XML,cc.VoyageObject) AS VoyageObjectXML
FROM [dbo].[tblVesselVoyageResultXML] cc
WHERE cc.VoyageResultID = b.VoyageResultID) c
ORDER BY b.VoyageID,b.VoyageResultID,b.IsActive
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER