我有两个用于代表市场的XML文件, 第一个文件是包含账单信息类型(销售,购买)的账单,这是文件:
<?xml version="1.0" encoding="utf-8"?>
<root>
<bill>
<billid>1</billid>
<billtype>sales</billtype>
<billdate>10/10/2013</billdate>
</bill>
<bill>
<billid>2</billid>
<billtype>Purchase</billtype>
<billdate>15/10/2013</billdate>
</bill>
<bill>
<billid>3</billid>
<billtype>Purchase</billtype>
<billdate>20/10/2013</billdate>
</bill>
<bill>
<billid>4</billid>
<billtype>sales</billtype>
<billdate>16/10/2013</billdate>
</bill>
</root>
,第二个文件包含该账单中的产品信息,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<root>
<itembill>
<itembill_id>1</itembill_id>
<itemid_ref>2</itemid_ref>
<billid_ref>2</billid_ref>
<price>20000</price>
</itembill>
<itembill>
<itembill_id>2</itembill_id>
<itemid_ref>3</itemid_ref>
<billid_ref>3</billid_ref>
<price>250000</price>
</itembill>
<itembill>
<itembill_id>4</itembill_id>
<itemid_ref>1</itemid_ref>
<billid_ref>2</billid_ref>
<price>140000</price>
</itembill>
<itembill>
<itembill_id>5</itembill_id>
<itemid_ref>2</itemid_ref>
<billid_ref>1</billid_ref>
<price>10000</price>
</itembill>
</root>
其中billid_ref是外键(billid),
问题是:
我希望 X查询为分组分组执行 SUM 帐单类型
我怎样才能做到这一点?!
我试过这个:
<catalog>
{
for $x in doc('bills')//bill,
$y in doc('itembills')//itembill where $y/billid_ref = $x/billid and $x/billtype = "Purchase"
return
<item >
{
sum ($y/price/text())
}
</item>
}
<catalog>
但效果不佳的结果是:
<catalog>
<item>20000</item>
<item>140000</item>
<item>250000</item>
<item>30000</item>
<catalog>
答案 0 :(得分:1)
xquery 3.0中有一个分组运算符,但xquery 1.0中没有。在1.0中,您可以创建一系列值并将它们相加:
<catalog>{
for $x in doc('bills')//bill
let $y := doc('itembills')//itembill where $y/billid_ref = $x/billid and $x/billtype = "Purchase"
return
<item >
{ sum ($y/price/text()) }
</item>
}</catalog>
但是如果没有其他循环(对于billtype in ...)
,您将无法按billtype分组