Xquery总和获得总额

时间:2018-11-04 13:00:26

标签: xml xquery

试图获取XQuery中订购的总量,但遇到一些问题。

这是OrderLine.xml

Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles        TextBox2.TextChanged
 if Trim(TextBox2.Text) = "" Then
 SpringDataBindingSource.Filter = ""
Else
 SpringDataBindingSource.Filter = "SerialNumber = '" + TextBox2.Text + "'"
End If
End Sub

我需要获取每个订单的总金额。这是我的xquery:

<?xml version="1.0" encoding="UTF-8" ?>
<dataroot>
 <OrderLine>
  <OrderNum>21608</OrderNum>
  <PartNum>AT94</PartNum>
  <NumOrdered>11</NumOrdered>
  <QuotedPrice>21.95</QuotedPrice>
 </OrderLine>
 <OrderLine>
  <OrderNum>21610</OrderNum>
   <PartNum>DR93</PartNum>
   <NumOrdered>1</NumOrdered>
   <QuotedPrice>495.00</QuotedPrice>
  </OrderLine>
  <OrderLine>
   <OrderNum>21610</OrderNum>
   <PartNum>DW11</PartNum>
   <NumOrdered>1</NumOrdered>
   <QuotedPrice>399.99</QuotedPrice>
  </OrderLine>
  <OrderLine>
    <OrderNum>21613</OrderNum>
    <PartNum>KL62</PartNum>
    <NumOrdered>4</NumOrdered>
    <QuotedPrice>329.95</QuotedPrice>
   </OrderLine>
   <OrderLine>
    <OrderNum>21614</OrderNum>
    <PartNum>KT03</PartNum>
    <NumOrdered>2</NumOrdered>
    <QuotedPrice>595.00</QuotedPrice>
   </OrderLine>
   <OrderLine>
    <OrderNum>21617</OrderNum>
    <PartNum>BV06</PartNum>
    <NumOrdered>2</NumOrdered>
    <QuotedPrice>794.95</QuotedPrice>
   </OrderLine>
   <OrderLine>
    <OrderNum>21617</OrderNum>
    <PartNum>CD52</PartNum>
    <NumOrdered>4</NumOrdered>
    <QuotedPrice>150.00</QuotedPrice>
   </OrderLine>
   <OrderLine>
    <OrderNum>21619</OrderNum>
    <PartNum>DR93</PartNum>
    <NumOrdered>1</NumOrdered>
    <QuotedPrice>495.00</QuotedPrice>
   </OrderLine>
   <OrderLine>
    <OrderNum>21623</OrderNum>
    <PartNum>KV29</PartNum>
    <NumOrdered>2</NumOrdered>
    <QuotedPrice>1290.00</QuotedPrice>
   </OrderLine>
  </dataroot>

当系统中只有一个订单时,它运行良好,但是当有多个订单时,我得到两个总金额。

我明白了:

  <results>
  {
    for $ord in doc("../premiere/Orders.xml")//Orders,
    $cus in doc("../premiere/Customer.xml")//Customer[CustomerNum = $ord/CustomerNum]    
return
    <row>
      <order number="{ $ord/OrderNum }" />
      <customer name="{ $cus/CustomerName }" />
      {
      let $ln := doc("../premiere/OrderLine.xml")//OrderLine[OrderNum = $ord/OrderNum]
      where $ord/CustomerNum = $cus/CustomerNum
      return
      <orderLine LineItems = "{ count($ln) }" /> 
      }
      {
      for $ln1 in doc("../premiere/OrderLine.xml")//OrderLine[OrderNum = $ord/OrderNum]
      let $total := ($ln1/NumOrdered * $ln1/QuotedPrice)
      return
      <orderRevenue TotalAmount= "{$total}" /> 
      }

    </row>
   }
  </results>

当我应该得到这个的时候:

 <row>
 <order number="21610"/>
 <customer name="Ferguson's"/>
 <orderLine LineItems="2"/>
 <orderRevenue TotalAmount="495"/>
 <orderRevenue TotalAmount="399.99"/>
 </row>

有帮助吗?

1 个答案:

答案 0 :(得分:0)

{
for $ln1 in doc("../premiere/OrderLine.xml")//OrderLine[OrderNum = $ord/OrderNum]
let $total := ($ln1/NumOrdered * $ln1/QuotedPrice)
return
  <orderRevenue TotalAmount= "{$total}" /> 
}

应替换为

{
let $x:= (
  for $ln1 in doc("../premiere/OrderLine.xml")//OrderLine[OrderNum = $ord/OrderNum]
  let $total := ($ln1/NumOrdered * $ln1/QuotedPrice)
  return
  <orderRevenue TotalAmount= "{$total}" /> )
  return <orderRevenue TotalAmount="{sum($x//@TotalAmount)}"/>
  }