Discussion:
[xquery-talk] FLWOR Question
Misztur, Chris
2014-01-11 18:13:23 UTC
Permalink
Hello.

Can the below code be improved to retrieve the current effective price (4000) given the following:

for $iap in $iaps-doc/iap (: for each iaps/iap :)
return
(
let $iap-name := $iap/@name/string() (: get the iap name :)
let $effective-price :=
(for $price in $iap/price
where current-date() >= xs:date($price/@effective/string()) (: select only price nodes where @effective is <= today's date :)
order by xs:date($price/@effective/string()) descending (: order descending so the first returned price node has the most recent date :)
return $price)[1]

return element { $iap-name } { $effective-price }
)

<iaps>
<iap name="bald">
<price amount="5000" amountText="!C5000" effective="01/01/2014"/>
<price amount="4000" amountText="!C4000" effective="01/05/2014"/>
<price amount="10000" amountText="!C10000" effective="01/15/2014"/>
</iap>
</iaps>

________________________________

The contents of this message may be privileged and confidential. Therefore, if this message has been received in error, please delete it without reading it. Your receipt of this message is not intended to waive any applicable privilege. Please do not disseminate this message without the permission of the author.

Please consider the environment before printing this e-mail
Christian Grün
2014-01-11 18:27:04 UTC
Permalink
Hi Chris,

> Can the below code be improved to retrieve the current effective price
> (4000) given the following:

each query processor may benefit from different optimizations, but
this is what I noticed:

> xs:date($price/@effective/string())

Your input (01/01/2014) doesn’t seem to be valid (2014-01-01); did you
already have some thoughts on this? If you work with a valid date
format, you could as well get rid of the date conversion, as all
values can also be sorted as strings, which is usually faster.

Next, it seems that you are requesting the effective price twice. It
could be faster to bind it to a variable instead. Your resulting query
could then look as follows:

let $current := string(current-date())
for $iap in //iap
let $effective-price := (
for $price in $iap/price
let $effective := $price/@effective/string()
where $current >= $effective
order by $effective descending
return $price
)[1]
return element { $iap/@name/string() } { $effective-price }

You may have noted that I have flattened the FLWOR expressions to a
single one. This is sth. that’s already done automatically by some
processors.

Hope this helps,
Christian
Misztur, Chris
2014-01-11 21:24:40 UTC
Permalink
The date format was my mistake.

Thank you for the explanation. That clears up the FLWOR acronym.

________________________________________
From: Christian Grün [***@gmail.com]
Sent: Saturday, January 11, 2014 12:27 PM
To: Misztur, Chris
Cc: ***@x-query.com
Subject: Re: [xquery-talk] FLWOR Question

Hi Chris,

> Can the below code be improved to retrieve the current effective price
> (4000) given the following:

each query processor may benefit from different optimizations, but
this is what I noticed:

> xs:date($price/@effective/string())

Your input (01/01/2014) doesn’t seem to be valid (2014-01-01); did you
already have some thoughts on this? If you work with a valid date
format, you could as well get rid of the date conversion, as all
values can also be sorted as strings, which is usually faster.

Next, it seems that you are requesting the effective price twice. It
could be faster to bind it to a variable instead. Your resulting query
could then look as follows:

let $current := string(current-date())
for $iap in //iap
let $effective-price := (
for $price in $iap/price
let $effective := $price/@effective/string()
where $current >= $effective
order by $effective descending
return $price
)[1]
return element { $iap/@name/string() } { $effective-price }

You may have noted that I have flattened the FLWOR expressions to a
single one. This is sth. that’s already done automatically by some
processors.

Hope this helps,
Christian

________________________________

The contents of this message may be privileged and confidential. Therefore, if this message has been received in error, please delete it without reading it. Your receipt of this message is not intended to waive any applicable privilege. Please do not disseminate this message without the permission of the author.

Please consider the environment before printing this e-mail
Loading...