我举个例子来更好地解释自己。
<shop>
<customers>
<customer id="1">
<level>A</level>
</kunde>
<customer id="2">
<level>A</level>
</kunde>
<customer id="3">
<level>B</level>
</kunde>
<customer id="4">
<level>C</level>
</kunde>
<customer id="5">
<level>C</level>
</kunde>
<customer id="6">
<level>C</level>
</kunde>
</customers>
<bills>
<bill id="1">
<customerId>1</customerId>
<bposition>
<price>1</price>
<amount>1</amount>
</bposition>
</bill>
<bill id="2">
<customerId>2</customerId>
<bposition>
<price>2</price>
<amount>2</amount>
</bposition>
<bposition>
<price>3</price>
<amount>2</amount>
</bposition>
</bill>
<bill id ="3">
<customerId>3</customerId>
<bposition>
<price>4</price>
<amount>1</amount>
</bposition>
</bill>
<bill id ="4">
<customerId>1</customerId>
<bposition>
<price>2</price>
<amount>2</amount>
</bposition>
</bill>
<bill id ="5">
<customerId>3</customerId>
<bposition>
<price>2</price>
<amount>2</amount>
</bposition>
<bposition>
<price>5</price>
<amount>2</amount>
</bposition>
</bill>
<bill id ="6">
<customerId>4</customerId>
<bposition>
<price>1</price>
<amount>3</amount>
</bposition>
</bill>
<bill id ="7">
<customerId>5</customerId>
<bposition>
<price>5</price>
<amount>1</amount>
</bposition>
</bill>
<bill id ="8">
<customerId>6</customerId>
<bposition>
<price>2</price>
<amount>1</amount>
</bposition>
</bill>
</bills>
</shop>
我需要得到总收入和每个客户的收入 按他们的级别分组。我可能需要distinct-values只得到一个级别,并像A、B、C那样升序排序。
解决的办法就是然后。
<level val="A">
<totalRevenue>18</totalRevenue>
<revenueperCustomer>7.5</revenueperCustomer>
</level>
<level val="B">
<totalRevenue>18</totalRevenue>
<revenueperCustomer>18</revenueperCustomer>
</level>
<level val="C">
<totalRevenue>10</totalRevenue>
<revenueperCustomer>3.33</revenueperCustomer>
</level>
到目前为止,我有什么。
let $s:= fn:doc('shop.xml')//customers/customer[level = 'A']/[@id],
$price := fn:doc('shop.xml')//bills/bill[customerId = $s]/bposition/price,
$amount := fn:doc('shop.xml')//bills/bill[customerId = $s]/bposition/amount
我怎样才能正确地按级别分组和排序呢?像这样
let $s:= fn:doc('shop.xml')//customers/customer/level
...
group by $s order by $s ascending
我不知道如何将级别分组,只选择正确的客户id,这样我就可以用正确的数值来计算。
下面是一个分组的例子。
for $bill at $pos in /shop/bills/bill
group by $cat := /shop/customers/customer[@id = $bill/customerId]/level
order by head($pos)
return
<level val="{$cat}">{
let $totalRevenue := sum($bill/bposition/(price * amount))
return (
<totalRevenue>{$totalRevenue}</totalRevenue>,
<revenueperCustomer>{$totalRevenue div count(distinct-values($bill/customerId))}</revenueperCustomer>
)
}
</level>
https:/xqueryfiddle.liberty-development.netpPqteB9。
或用一个加载了 doc
功能
declare context item := doc('shop.xml');
for $bill at $pos in /shop/bills/bill
group by $cat := /shop/customers/customer[@id = $bill/customerId]/level
order by head($pos)
return
<level val="{$cat}">{
let $totalRevenue := sum($bill/bposition/(price * amount))
return (
<totalRevenue>{$totalRevenue}</totalRevenue>,
<revenueperCustomer>{$totalRevenue div count(distinct-values($bill/customerId))}</revenueperCustomer>
)
}
</level>