I have a xml file like below, and i wanted to group the toyID and get the sum of how much that toy has been sold. and order it by the toyID with the highest sales.but the code that i wrote is able to sum the amount of toy sold,but the order by the highest amount of toy sold is not working.can i know which part i did wrongly?
ToySale.XML
<toySale companyID="1" toyID="11" >
<amount>15</amount>
</toySale>
<toySale companyID="3" toyID="11" >
<amount>12</amount>
</toySale>
<toySale companyID="1" toyID="22" >
<amount>3</amount>
</toySale>
<toySale companyID="2" toyID="33" >
<amount>7</amount>
</toySale>
below is the code that i wrote:
for $t in distinct-values(ToySale.xml")//@toyID)
return <toyID>
toyID : {$t}
<totalSale>{sum(for $sum in (ToySale.xml")//toySale
order by $sum/@toyID/../amount
where $t=$sum/@toyID
return $sum/@productID/../amount) }
</totalSale
</toyID>
This query:
When applied on the provided XML document:
produces the wanted, correct result:
An equivalent and slightly shorter query: