XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
sap pageSubject: Xquery find avg and count
Author: sap page
Date: 29 May 2006 08:23 AM
Originally Posted: 29 May 2006 08:05 AM
I currently have a bit of a problem with my uni assignment and was wondering if anyone could help. This was about the only xquery site i could find a forum on.

Write a query to find all products whose sale price is more than twice the average price of some other product within the same type. Display the total number of products with such high prices for each type. Also display the name and price of these products. For each type, the product must be listed in increasing order by price.

output

<Expensive-products>
<Expensive-products-on-Microsoft>
<Total-number> . </Total-number>
<product>
<name> . </name>
<price> . </price>
<product>
<product> .</product>
</ Expensive-products-on-Microsoft>

</Expensive-products>

products

<products>
<product type ="microsoft_technologies">
<name> VB </name>
<price> 100 </price>
<number> 50 </number>
</product>

Postnext
sap pageSubject: Xquery
Author: sap page
Date: 29 May 2006 08:10 AM
for $products in distinct-values(doc("products.xml")/products/product/@type)
return

for $type in doc("products.xml")/products/product[@type=$products]
let $avg_price := doc("products.xml")/products/product[@type=$type/@type]/price/text()
let $avg := doc("products.xml")/products/product[@type=$products]
for $t in $avg/price/text()
where avg($t)*2 < $type/price/text()
return count($t)

This is what I have so far just so you dont think i am using the forums to get free answers!

Posttop
Ivan PedruzziSubject: Xquery
Author: Ivan Pedruzzi
Date: 29 May 2006 10:38 PM

Is something like the following works for you?

for $type in distinct-values(doc("products.xml")/products/product/@type)
let $products_by_type := doc("products.xml")/products/product[@type=$type]
let $products_count := count($products_by_type)
let $price_average := avg($products_by_type/price/text())
let $expensive_products := $products_by_type[price/text() > $price_average * 2]
return
(
count($expensive_products),
for $product in $expensive_products
return $product/name/text()
)

Hope this helps
Ivan Pedruzzi
Stylus Studio Team

 
Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.