xQuery distinct-value sum issue -


i need sum average price of specific product categories, have managed merge same category types , count them, need sum prices different categories (currently have selected prices in query)

for example: if there 5 products of category need select 5 prices , sum them up.

query:

let $hk := doc('http://etutor.dke.uni-linz.ac.at/etutor/xml?id=1')/handelskette/produkte/produkt  $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 grateful help

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 xpath 2.0 expression (and xquery) evaluated against 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 average prices each category) produced:

2397.5 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -