Sum of XML duration elements in SQL2008

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-03-24T20:10:51Z Indexed on 2010/03/24 20:13 UTC
Read the original article Hit count: 458

Filed under:
|
|
|

I have a XML column that holds information about my games. Here's a sample of the information looks like.

<game xmlns="http://my.name.space" >
<move>
    <player>PlayerA</player>
    <start movetype="Move">EE5</start>
    <end movetype="Move">DF6</end>
    <movetime>PT1S</movetime>
</move>
<move>
    <player>PlayerB</player>
    <start movetype="Move">CG7</start>
    <end movetype="Move">DE6</end>
    <movetime>PT3S</movetime>
</move>
<move>
    <player>PlayerA</player>
    <start movetype="Move">FD3</start>
    <end movetype="Move">EG8</end>
    <movetime>PT4S</movetime>
</move>
</game>

I'm trying to design an XML query to take the sum of my movetime element. Basically I need the sum of each players move time. So using the above sample, PlayerA would have a total move time of 5 seconds and PlayerB would have a total move time of 3 seconds.

Here's the XML query that I've been currently been working with

SELECT GameHistory.query('declare default element namespace "http://my.name.space"; data(/game/move/movetime)') AS Value FROM GamesWHERE Id=560

I'm a newbie to XSLT / XPATH functions :P

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about xpath