title: XQuery: Getting average price of products grouped by category
body: <p>My database (xml) is the following:</p>
  
  <pre><code>&lt;db&gt;
      &lt;productLine&gt;
          &lt;product nr="4567"&gt;
              &lt;name&gt;Shampoo&lt;/name&gt;
              &lt;category&gt;Care&lt;/category&gt;
          &lt;/product&gt;
          &lt;product nr="4568"&gt;
              &lt;name&gt;Polish&lt;/name&gt;
              &lt;category&gt;Care&lt;/category&gt;
          &lt;/product&gt;
          &lt;product nr="7010"&gt;
              &lt;name&gt;Radio&lt;/name&gt;
              &lt;category&gt;Electronic&lt;/category&gt;
          &lt;/product&gt;
          &lt;product nr="7012"&gt;
              &lt;name&gt;Headphones&lt;/name&gt;
              &lt;category&gt;Electronic&lt;/category&gt;
          &lt;/product&gt;
      &lt;/productLine&gt;
      &lt;stores&gt;
          &lt;store storeNr="1"&gt;
              &lt;range&gt;
                  &lt;prodInRange&gt;
                      &lt;nr&gt;4567&lt;/nr&gt;
                      &lt;salesPrice&gt;20&lt;/salesPrice&gt;
                  &lt;/prodInRange&gt;
                  &lt;prodInRange&gt;
                      &lt;nr&gt;4568&lt;/nr&gt;
                      &lt;salesPrice&gt;40&lt;/salesPrice&gt;
                  &lt;/prodInRange&gt;
                  &lt;prodInRange&gt;
                      &lt;nr&gt;7010&lt;/nr&gt;
                      &lt;salesPrice&gt;120&lt;/salesPrice&gt;
                  &lt;/prodInRange&gt;
              &lt;/range&gt;
          &lt;/store&gt;
          &lt;store storeNr="2"&gt;
              &lt;range&gt;
                  &lt;prodInRange&gt;
                      &lt;nr&gt;4567&lt;/nr&gt;
                      &lt;salesPrice&gt;25&lt;/salesPrice&gt;
                  &lt;/prodInRange&gt;
                  &lt;prodInRange&gt;
                      &lt;nr&gt;7010&lt;/nr&gt;
                      &lt;salesPrice&gt;140&lt;/salesPrice&gt;
                  &lt;/prodInRange&gt;
                  &lt;prodInRange&gt;
                      &lt;nr&gt;7012&lt;/nr&gt;
                      &lt;salesPrice&gt;50&lt;/salesPrice&gt;
                  &lt;/prodInRange&gt;
              &lt;/range&gt;
          &lt;/store&gt;
      &lt;/stores&gt;
  &lt;/db&gt;
  </code></pre>
  
  <p>I want to show the average price of products grouped by their <strong>CATEGORY</strong>:</p>
  
  <p>Wanted Output:</p>
  
  <pre><code>&lt;category value="Care"&gt;28.3333&lt;/category&gt;
  &lt;category value="Electronic"&gt;103.3333&lt;/category&gt;
  </code></pre>
  
  <p>Process: Avg of (20, 25, 40) and Avg of (120, 140, 50)</p>
  
  <p>I already have the following:</p>
  
  <pre><code>let $db := doc("avgCategory.xml")/db
  
  for $pro in $db/productLine/product
  let $cat := $pro/category
  group by $cat
  
  return 
  &lt;category val="{$cat}"&gt;&lt;/category&gt;
  </code></pre>
  
  <p>But I don't know how to output the avg price of the product categories.</p>