Sum of XML duration elements in SQL2008
- by Matt
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