sum some xml nodes values in sql server 2008

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

Please consider this XML:


    10 
    20 
    

        
4条回答
  •  有刺的猬
    2021-01-13 19:49

    Try this one -

    DECLARE @XML XML
    SELECT @XML = '
    
        10 
        20 
        0 
    
    
        15 
        20 
        5 
    '
    
    SELECT 
          value = SUM(t.value)
        , t.id 
    FROM (
        SELECT 
              value = p.value('.', 'INT')
            , id = p.value('./../@ID', 'VARCHAR(50)')
        FROM @XML.nodes('/Parent/Child') t(p)
    ) t
    --WHERE t.id = 'p'
    GROUP BY t.id
    

提交回复
热议问题