How do I write an XPath sum over the following structure ?
<Order>
<Details>
<Detail>
<Quantity>10</Quantity>
<ItemPrice>20</Quantity>
</Detail>
<Detail>
<Quantity>10</Quantity>
<ItemPrice>20</Quantity>
</Detail>
</Details>
</Order>
I want to get the sum of (quantity * itemprice)
I can do this:
select *
from mytable
where documentdata.exist('/Order[sum(/Details/Detail/Quantity) > 20]) = 1
But I want to use Quantity * ItemPrice but I cant figure out the syntax for it..
Try
select *
from mytable
where documentdata.exist('/Order[sum(for $d in Details/Detail return $d/Quantity * $d/ItemPrice) > 20]) = 1
That is the right XQuery syntax so it might work with MS SQL Server as it has some limited XQuery support.
I think you want this XPath 2.0 expression:
/Order[sum(Details/Detail/(Quantity * ItemPrice)) gt 20]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With