sum some xml nodes values in sql server 2008

前端 未结 4 720
傲寒
傲寒 2021-01-13 19:24

Please consider this XML:


    10 
    20 
    

        
4条回答
  •  囚心锁ツ
    2021-01-13 19:52

    Try this :-

     Declare @xml xml 
    set @xml='
             10 
             20 
             0 
              '
    
     Select @xml.value('sum(/Parent/Child)','int') as Sum
    

    Result : 30

    or if you want the sum for a specific Parent ID then try the below query

     Select @xml.value('sum(/Parent/Child)','int') AS SumVal
     where @xml.exist('/Parent[@ID="p"]') = 1;
    

    Demo in SQL FIDDLE

提交回复
热议问题