2012-03-12 59 views
0

即時通過XQuery來計算兩年內的銷售量,它基於SQL查詢。將SQL查詢轉換爲計算雙年銷售額的XQuery。

SELECT [ShipCountry] & " " & [ShipCity] AS CountryAndCity, COUNT(OrderID) AS BiYearlySales 
FROM Orders 
WHERE (((OrderDate) BETWEEN #1/1/1996# And #1/1/1998#)) 
GROUP BY [ShipCountry] & " " & [ShipCity]; 

這是我到目前爲止的xquery,但它只計算每個實例一次。不能找出原因。 我正在使用Altova xmlspy - xquery 1.0。

<Result> 
{ 
for $a in doc("Orders.xml")/dataroot/Orders 
let $b := concat($a/ShipCountry, " ", $a/ShipCity) 
where $a/OrderDate >= '1996-01-01T00:00:00' and $a/OrderDate <= '1998-01-01T00:00:00' 
return 
<BiYearlySales> 
    <CountryNameAndCity>{$b}</CountryNameAndCity> 
    <OrderCount>{count($a)}</OrderCount> 
</BiYearlySales> 
} 
</Result> 

我意識到我還沒有完成組,但試圖讓這個工作更重要。

+0

僅供參考,我們有兩個人要求完全相同的作業幫助。查看標記爲xquery的其他最近問題可能會有幫助。 – 2012-03-13 00:20:01

回答

0

不知道這是完全正確的......(還沒有嘗試過),但這樣的事情:

for $country in doc("Orders.xml")/dataroot/Orders[OrderDate ge '1996-01-01T00:00:00' and OrderDate le '1998-01-01T00:00:00']/ShipCountry, $city in doc(doc("Orders.xml")/dataroot/Orders[OrderDate ge '1996-01-01T00:00:00' and OrderDate le '1998-01-01T00:00:00']/ShipCity") 
return 
<sales> 
    <geo>{$country, " ", $city)}</geo> 
    <count>{count(doc("Orders.xml")/dataroot/Orders 
      [OrderDate ge '1996-01-01T00:00:00' and OrderDate le '1998-01-01T00:00:00' 
      and ShipCountry eq $country and ShipCity eq $city])} 
    </count> 
</sales>