Reputation: 25
I need to sum up the average price of specific product categories, currently i have managed to merge the same category types and count them, now i need to sum up all the prices from the different categories (currently i have selected all prices in my query)
for example: if there are 5 Products of Category A i need to select those 5 prices and sum them up.
Query:
let $hk := doc('http://etutor.dke.uni-linz.ac.at/etutor/XML?id=1')/handelskette/produkte/produkt
for $x in distinct-values($hk/kategorie)
let $c := count($hk[kategorie eq $x])
let $a:= sum($hk/ekPreis)
return <kategorie kname="{$x}">
<anzahl>{$c}</anzahl>
<avgEkPreis>{$a div $c}</avgEkPreis>
</kategorie>
XML:
<handelskette>
<produkte>
<produkt ean="0-666-4567-2-22">
<bezeichnung>Autoschampoo</bezeichnung>
<kategorie>Pflege</kategorie>
<ekPreis>35</ekPreis>
<listPreis>69</listPreis>
</produkt>
<produkt ean="0-777-4997-2-43">
<bezeichnung>Glanzpolitur</bezeichnung>
<kategorie>Pflege</kategorie>
<ekPreis>70</ekPreis>
<listPreis>119</listPreis>
</produkt>
<produkt ean="1-4444-652-8-88">
<bezeichnung>CD-Wechsler</bezeichnung>
<kategorie>Audio</kategorie>
<ekPreis>2345</ekPreis>
<listPreis>3999</listPreis>
</produkt>
</produkte>
</handelskette>
I would be very grateful for any help
Upvotes: 1
Views: 414
Reputation: 1577
Given your processor handles XQuery 3.0 you might give group by
a try for improved readability:
for $produkt in doc('http://etutor.dke.uni-linz.ac.at/etutor/XML?id=1')/handelskette/produkte/produkt
group by $kname := $produkt/kategorie
let $anzahl := count($produkt)
let $avg_ek := sum($produkt/ekPreis) div $anzahl
return
<kategorie kname="{ $kname }">
<anzahl>{ $anzahl }</anzahl>
<avgEkPreis>{ $avg_ek }</avgEkPreis>
</kategorie>
Upvotes: 0
Reputation: 25
I didn't explain it exactly the right way, my english isn't so good^^. i meant that i needed each category for its own summed up, but thanks to you i got it done with parts of your posted syntax.
here is my query now - if someone has a similar issue:
let $hk := doc('http://etutor.dke.uni-linz.ac.at/etutor/XML?id=1')/handelskette/produkte/produkt
for $x in distinct-values($hk/kategorie)
let $c := count($hk[kategorie eq $x])
let $a:= sum($hk[kategorie eq $x]/ekPreis)
return <kategorie kname="{$x}">
<anzahl>{$c}</anzahl>
<avgEkPreis>{$a div $c}</avgEkPreis>
</kategorie>
Thank you very much and have a nice week!
Upvotes: 0
Reputation: 243579
Simply use:
sum(for $vCat in distinct-values(/*/*/*/kategorie),
$vCount in count(/*/*/produkt[kategorie eq $vCat])
return
sum(/*/*/produkt[kategorie eq $vCat]/ekPreis/xs:double(.)) div $vCount
)
When this XPath 2.0 expression (and also a XQuery) is evaluated against the provided XML document:
<handelskette>
<produkte>
<produkt ean="0-666-4567-2-22">
<bezeichnung>Autoschampoo</bezeichnung>
<kategorie>Pflege</kategorie>
<ekPreis>35</ekPreis>
<listPreis>69</listPreis>
</produkt>
<produkt ean="0-777-4997-2-43">
<bezeichnung>Glanzpolitur</bezeichnung>
<kategorie>Pflege</kategorie>
<ekPreis>70</ekPreis>
<listPreis>119</listPreis>
</produkt>
<produkt ean="1-4444-652-8-88">
<bezeichnung>CD-Wechsler</bezeichnung>
<kategorie>Audio</kategorie>
<ekPreis>2345</ekPreis>
<listPreis>3999</listPreis>
</produkt>
</produkte>
</handelskette>
the wanted, correct result (the sum of the average prices for each category) is produced:
2397.5
Upvotes: 1