XQuery - math on elements within a sequence

Monday, 15 June 2015 · 2 messages
CG
Christian Grünin reply to Luiz Matos, 15 Jun 2015, 23:2615 Jun 2015 21:39 #
Hi Luiz,
Quoted text · Luiz Matos · 2 lines
> My question: Considering this dataset model, how can I to write this query > without change its semantics?
I think you'll need to change its semantics, or need to explain a bit more what you trying to do. See the following query as example: XQUERY:
  for $lineitem in <xml>
    <LineItem>
      <Discount>0.0913</Discount>
      <ReturnFlag>A</ReturnFlag>
    </LineItem>
    <LineItem>
      <Discount>0.0674</Discount>
      <ReturnFlag>A</ReturnFlag>
    </LineItem>
  </xml>/LineItem
  group by $returnflag := $lineitem/ReturnFlag
  return <record>{ $lineitem/Discount }</record>
RESULT:
<record>
  <Discount>0.0913</Discount>
  <Discount>0.0674</Discount>
</record>
The returned record contains two Discount elements. In your original query, you are trying subtracting two values from a single number (1-$lineitem/Discount), so I am not sure what result you expect here? Maybe this already answers your question?
Quoted text · Luiz Matos · 2 lines
> I wouldn't want to use extra functions (e.g., fn:for-each), because I need > to submit the same query in an limited xquery processor.
For general questions on XQuery, the talk at x-query.com mailing list may be a better choice. Best, Christian
LM
Luiz Matos15 Jun 2015 23:26 #
Hi everyone, I'm trying to execute a XQuery sum function in a multiplication of two XML elements, but it has been difficult to avoid the iteration in a sequence of elements. For example, considering this case: sample data:
<Orders>
     <Order>
        <OrderKey>1</OrderKey>
        <LineItem>
          <LineNumber>1</LineNumber>
          <ExtendedPrice>70848.0000</ExtendedPrice>
          <Discount>0.0913</Discount>
          <ReturnFlag>A</ReturnFlag>
          <LineStatus>F</LineStatus>
          <ShipDate>1994-09-03</ShipDate>
        </LineItem>
        <LineItem>
          <LineNumber>2</LineNumber>
          <ExtendedPrice>64372.0000</ExtendedPrice>
          <Discount>0.0674</Discount>
          <ReturnFlag>A</ReturnFlag>
          <LineStatus>F</LineStatus>
          <ShipDate>1994-08-18</ShipDate>
        </LineItem>
        <LineItem>
          <LineNumber>3</LineNumber>
          <ExtendedPrice>64247.0000</ExtendedPrice>
          <Discount>0.0396</Discount>
          <ReturnFlag>R</ReturnFlag>
          <LineStatus>F</LineStatus>
          <ShipDate>1994-10-21</ShipDate>
        </LineItem>
      </Order>
      . . .
</Orders>
XQuery:
<results> {
   for $lineitem in collection("tpch")/Orders/Order/LineItem
   let $returnflag := $lineitem/ReturnFlag
   let $linestatus := $lineitem/LineStatus
   where $lineitem/ShipDate <= "1998-08-31"
   group by $returnflag, $linestatus
   order by $returnflag, $linestatus
   return
   <record>
      <sum_disc_price>{sum($lineitem/ExtendedPrice*(1-$lineitem/Discount))}</sum_disc_price>
   </record>
} </results>
BaseX output: [XPTY0004] Item expected, sequence found: (element ExtendedPrice {...}, ...... My question: Considering this dataset model, how can I to write this query without change its semantics? I wouldn't want to use extra functions (e.g., fn:for-each), because I need to submit the same query in an limited xquery processor. Thanks in advance!
-- Luiz Matos PhD student at Federal Fluminense University

⚡Generated with XQuery