Link to home
Start Free TrialLog in
Avatar of Paul Mauriello
Paul MaurielloFlag for United States of America

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?




<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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Paul Mauriello

ASKER

Thnak you just what I needed!! :)