Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath Sum over sub structure

Tags:

xml

xpath

xquery

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..

like image 608
Roger Johansson Avatar asked Mar 28 '26 19:03

Roger Johansson


2 Answers

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.

like image 126
Martin Honnen Avatar answered Apr 01 '26 11:04

Martin Honnen


I think you want this XPath 2.0 expression:

/Order[sum(Details/Detail/(Quantity * ItemPrice)) gt 20]

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!