XQuery/XPath: 如何通过group by函数获取计算值
XQuery/XPath: How to get calculated value through group by function
我会举个例子来更好地解释我自己:
<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
我不知道如何对级别进行分组,只知道 select 正确的客户 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.net/pPqteB9
或使用 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>
我会举个例子来更好地解释我自己:
<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
我不知道如何对级别进行分组,只知道 select 正确的客户 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.net/pPqteB9
或使用 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>