2012-03-12 87 views
0

我嘗試轉換下面的SQL總和與GROUP BY的XQuery

SELECT SUM((((1-Discount)*Quantity)*[Unit Price])) AS AMOUNT 
FROM Orders 
LEFT JOIN [Order Details] 
    ON Orders.[Order ID] = [Order Details].[Order ID] 
WHERE ((([Order Date]) BETWEEN #1/1/2006# AND #2/1/2006#)); 

的訂單和訂單明細有1一對多的關係(1是訂單)

我目前擁有的代碼顯示每個訂單詳細信息的全部價格,但是不會允許我將每個訂單詳細信息的總價值合併在一起以獲取完整價值。樣品Orders.xml

<OrderID>10248</OrderID> 
     <CustomerID>WILMK</CustomerID> 
     <EmployeeID>5</EmployeeID> 
     <OrderDate>1996-07-04T00:00:00</OrderDate> 
     <RequiredDate>1996-08-01T00:00:00</RequiredDate> 
     <ShippedDate>1996-07-16T00:00:00</ShippedDate> 
     <ShipVia>3</ShipVia> 
     <Freight>32.38</Freight> 
     <ShipName>Vins et alcools Chevalier</ShipName> 
     <ShipAddress>59 rue de l&apos;Abbaye</ShipAddress> 
     <ShipCity>Reims</ShipCity> 
     <ShipPostalCode>51100</ShipPostalCode> 
     <ShipCountry>France</ShipCountry> 
    </Orders> 
    <Orders> 
     <OrderID>10249</OrderID> 
     <CustomerID>TRADH</CustomerID> 
     <EmployeeID>6</EmployeeID> 
     <OrderDate>1996-07-05T00:00:00</OrderDate> 
     <RequiredDate>1996-08-16T00:00:00</RequiredDate> 
     <ShippedDate>1996-07-10T00:00:00</ShippedDate> 
     <ShipVia>1</ShipVia> 
     <Freight>11.61</Freight> 
     <ShipName>Toms Spezialitäten</ShipName> 
     <ShipAddress>Luisenstr. 48</ShipAddress> 
     <ShipCity>Münster</ShipCity> 
     <ShipPostalCode>44087</ShipPostalCode> 
     <ShipCountry>Germany</ShipCountry> 
    </Orders> 

打樣訂單的`的

for $orderdetails in doc("Order Details.xml")/dataroot/Order_x0020_Details 
let $order := doc("Orders.xml")/dataroot/Orders[OrderID = $orderdetails/OrderID] 

where xs:dateTime($order/OrderDate/text()) gt xs:dateTime("1996-04-06T00:00:00") and xs:dateTime($order/OrderDate/text()) lt xs:dateTime("1997-04-05T00:00:00") 

return 
<Invoice_Amounts> 
{ 
    $order/OrderID 
} 
<Amount> 
{ 
sum(((1 - xs:double($orderdetails/Discount)) * xs:double($orderdetails/UnitPrice)) * xs:double($orderdetails/Quantity)) 
} 
</Amount> 
</Invoice_Amounts> 

Details.xml

<OrderID>10248</OrderID> 
<ProductID>11</ProductID> 
<UnitPrice>14</UnitPrice> 
<Quantity>12</Quantity> 
<Discount>0</Discount> 
</Order_x0020_Details> 
<Order_x0020_Details> 
<OrderID>10248</OrderID> 
<ProductID>42</ProductID> 
<UnitPrice>9.8</UnitPrice> 
<Quantity>10</Quantity> 
<Discount>0</Discount> 
</Order_x0020_Details> 
<Order_x0020_Details> 
<OrderID>10248</OrderID> 
<ProductID>72</ProductID> 
<UnitPrice>34.8</UnitPrice> 
<Quantity>5</Quantity> 
<Discount>0</Discount> 
</Order_x0020_Details> 
<Order_x0020_Details> 
<OrderID>10249</OrderID> 
<ProductID>14</ProductID> 
<UnitPrice>18.6</UnitPrice> 
<Quantity>9</Quantity> 
<Discount>0</Discount> 
</Order_x0020_Details> 
+0

您定位的是哪個版本的標準? (XQuery 3.0通過支持顯式分組)。 – 2012-03-12 21:13:20

+0

如果代碼樣本被減少到與這個特定問題相關的最小值,這將是有幫助的;現在通過閱讀相關細節需要做很多工作。 – 2012-03-12 21:14:14

+0

xml文檔超過20000行,因此我只提出了一些示例文本,但是我在XML Spy中使用了X Query 1 – 2012-03-12 21:55:20

回答

0

所以 - 據我所知,你已經知道該怎麼做的加入,所以要重點關注的是總和操作。要做到這一點,最簡單的方法是使用一個循環設置一個變量,然後您可以使用:

let $doc := 
    <docroot> 
     <Order id="1"> 
      <Discount>0.05</Discount> 
      <Quantity>3</Quantity> 
      <UnitPrice>15</UnitPrice> 
     </Order> 
     <Order id="2"> 
      <Discount>0</Discount> 
      <Quantity>15</Quantity> 
      <UnitPrice>20</UnitPrice> 
     </Order> 
    </docroot> 

(: collect the orders relevant to your query; 
    in your "real" version you'll want to filter, 
    or do your join, or whatever :) 
let $orders := $doc/Order 

(: generate a sequence containing the price for 
    each such order :) 
let $order_costs := 
    for $order in $orders 
     return ((1.0 - xs:double($order/Discount)) 
        * xs:double($order/UnitPrice) 
        * xs:double($order/Quantity)) 

(: generate your final result :) 
return sum($order_costs) 

您還可以通過參數中嵌入的for循環sum()避免變量賦值:

return sum(for $order in $orders 
      return ((1.0 - xs:double($order/Discount)) 
        * xs:double($order/UnitPrice) 
        * xs:double($order/Quantity)) 
+0

查爾斯,我appretiate你的答案,但讓$訂單標記「XQuery驗證錯誤!」 意外的令牌 - 「let $ orders:= $ orderdeta」 – 2012-03-12 22:25:23

+0

嗯,我在這裏沒有可用的XQuery 1.0引擎,它在BaseX上工作正常,所以我需要以後再回復你 – 2012-03-12 22:26:55

+0

@PeterRoche我給出的確切查詢沒有'let $ orders:= $ orderdeta'行,並且已經驗證可以在XMLSpy上工作。 – 2012-03-13 13:41:36