将SQL查询转换为计算双年销售额的XQuery。

时间:2012-03-12 20:50:02

标签: xquery xquery-sql

我无法获得一个XQuery来计算两年以上的销售额,它基于SQL查询。

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>

我意识到我还没有完成这个小组,但试图让这个工作更重要。

1 个答案:

答案 0 :(得分: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>