XQuery 嵌套查询

XQuery Nested Query

我不知道如何执行这个查询:

使用 XQuery, 对于每个代表编号,输出:

一个。代表的 名字姓氏

乙。 与代表关联的每个客户的客户名称

C.总金额(不含税)针对与代表相关联的所有客户

Premiere 数据库架构:

-A Customer 结构具有 CustomerNumber、RepNum(分配的代表)等属性

-A Rep结构有属性RepNum等

-Orders 结构具有 CustomerNumber、OrderNum 等属性

-OrderLine 结构具有属性 OrderNum、NumOrdered、QuotedPrice(per -item) 等

*多个订单行可以有相同的订单号。

示例所需输出:

Q4输出:

<results>
  <rep repnum="20" firstname="Valerie" lastname="Kaiser">
    <customer name="Al's Appliance and Sport"/>
    <customer name="Kline's"/>
    <customer name="All Season"/>
    <Revenue total="6.45"/>
  </rep>
  <rep repnum="35" firstname="Richard" lastname="Hull">
    <customer name="Brookings Direct"/>
    <customer name="The Everything Shop"/>
    <customer name="Lee's Sport and Appliance"/>
    <customer name="Deerfield's Four Seasons"/>
    <Revenue total=",509.80"/>
  </rep>
  <rep repnum="65" firstname="Juan" lastname="Perez">
    <customer name="Ferguson's"/>
    <customer name="Bargains Galore"/>
    <customer name="Johnson's Department Store"/>
    <Revenue total=",664.89"/>
  </rep>
</results>

我得到的输出:

<results>
  <rep>
    <rep repnum="20" firstname="Valerie" lastname="Kaiser"/>
    <customer name="Al's Appliance and Sport"/>
    <customer name="Kline's"/>
    <customer name="All Season"/>
    <Revenue total="0"/>
  </rep>
  <rep>
    <rep repnum="35" firstname="Richard" lastname="Hull"/>
    <customer name="Brookings Direct"/>
    <customer name="The Everything Shop"/>
    <customer name="Lee's Sport and Appliance"/>
    <customer name="Deerfield's Four Seasons"/>
    <Revenue total="0"/>
  </rep>
  <rep>
    <rep repnum="65" firstname="Juan" lastname="Perez"/>
    <customer name="Ferguson's"/>
    <customer name="Bargains Galore"/>
    <customer name="Johnson's Department Store"/>
    <Revenue total="0"/>
  </rep>
</results>

每个代表的收入为 0!

我的代码:

<results>
  {
    for $r in doc("../premiere/Rep.xml")//Rep
    let $c := doc("../premiere/Customer.xml")//Customer[RepNum = $r/RepNum]

    return 
      <rep>
        <rep repnum = "{$r/RepNum}" firstname="{$r/FirstName}" lastname="{$r/LastName}"/>

        {for $customer in $c 
        return
           <customer name= "{ $customer/CustomerName}"/>}

        <Revenue total=  "{sum(
             for $customer in $c 
             let $o := doc("../premiere/Orders.xml")//Orders[CustomerNumber = $customer/CustomerNumber]
             let $customerAmount := sum(
                 for $order in $o
                 let $l := doc("../premiere/OrderLine.xml")//OrderLine[OrderNum = $order/OrderNum]
                 let $orderAmount := format-number(sum(
                     for $lineItem in $l
                     let $LineAmount := (data($lineItem/NumOrdered) * data($lineItem/QuotedPrice))
                     return 
                       $LineAmount
                 ), '$,000.00')
                 return
                   $orderAmount
             )
             return
               $customerAmount
         )}"
         />
      </rep>

  }
</results>

提前致谢!

首先,正如@joewiz 指出的那样,在对它们求和之前将部分和格式化为字符串,如果执行,这肯定会引发错误。既然没有,我怀疑外部总和永远不会加起来任何值。

您没有提供任何示例数据,但我发现 this SQL filePremiere 数据集似乎与您的匹配。一个重要的区别是 CUSTOMER 具有属性 CUSTOMER_NUM,而不是您描述中的 CUSTOMER_NUMBER。如果该元素在您的数据集中也被称为 CustomerNum 而不是 CustomerNumber,则 let $o := doc("../premiere/Orders.xml")//Orders[CustomerNumber = $customer/CustomerNumber] 将静默计算为空序列,总和为 0.

这是您的查询的一个版本,它适用于我,并在根据 SQL 文件中的值进行评估时生成您想要的准确输出:

<results>{
  for $r in doc("../premiere/Rep.xml")//Rep
  let $c := doc("../premiere/Customer.xml")//Customer[RepNum = $r/RepNum]
  return <rep>{
      <rep repnum = "{$r/RepNum}" firstname="{$r/FirstName}" lastname="{$r/LastName}"/>,

      for $customer in $c 
      return <customer name="{$customer/CustomerName}"/>,

      let $total :=
        sum(
          for $customer in $c
          for $order in doc("../premiere/Orders.xml")//Orders[CustomerNum = $customer/CustomerNumber]
          for $lineItem in doc("../premiere/OrderLine.xml")//OrderLine[OrderNum = $order/OrderNum]
          return $lineItem/NumOrdered * $lineItem/QuotedPrice
        )
      return <Revenue total="{format-number($total, '$,000.00')}"/>
  }</rep>
}</results>

我通过将两个总和合并为一个并内联一些变量来简化它,并将数字格式移到正确的位置。