Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 674149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:40:26+00:00 2026-05-14T00:40:26+00:00

Take a simple OrderDetail table which has a Quantity and UnitPrice for each record.

  • 0

Take a simple OrderDetail table which has a Quantity and UnitPrice for each record. To get the total value of each Order with SQL it’s simple as

SELECT OrderID, SUM(UnitPrice*Quantity)
FROM OrderDetail
GROUP BY OrderID

After converting the table to a XML file, using XQUERY I’m able to get the same information like this

for $orderId in distinct-values(doc('orderDetails.xml')//orderDetails/OrderID)
   let $totalValue := 
      sum(
      for $detail in doc("orderdetails.xml")//OrderDetails[OrderID =$orderId]
         return $detail/Quantity * $detail/UnitPrice
      )
return <order id="{$orderId}" totalValue="{$totalValue}" />

Ignoring the sheer stupidity of getting the information like this, is there a better way to write the XQUERY expression? As it is it’s very, very slow (I’m using XMLSpy).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:40:27+00:00Added an answer on May 14, 2026 at 12:40 am

    This is a major flaw in XQuery 1.0, and for this reason a group-by clause has been added in XQuery 1.1, they have added a group by clause, so your query would look like this:

    for $orderDetails in doc('orderDetails.xml')//OrderDetails)
    let $orderId = $orderDetails/OrderID
    let $orderCost = $orderDetails/Quantity * $orderDetails/UnitPrice
    group by $orderId
    let $totalValue := sum($orderCost)
    return <order id="{$orderId}" totalValue="{$totalValue}" />
    

    Unfortunately however, XQuery 1.1 is still just a working draft, and few implementations are available.

    Our implementation (XQSharp) tries to spot the pattern you used and do the group by more efficiently (this shows up as a group-by in the query plan). Unfortunately though, our implementation does not spot a group by in your particular case.

    The first problem is casing differences (“orderdetails.xml” vs “orderDetails.xml”, //orderDetails vs //OrderDetails) – I’ll assume these are just typos.

    The biggest problem is that what you have written is not a trivial group by!

    Unless you are using a schema which indicates otherwise, static analysis cannot determine that each node has exactly one OrderID, and the atomized value of OrderID may have more than one item (if it has a list as its schema type). This means that static analysis of distinct-values(doc('orderDetails.xml')//orderDetails/OrderID) cannot determine that each node has exactly one key.

    In order to fix this your query can be written as follows:

    for $orderId in distinct-values(doc("orderDetails.xml")/OrderDetails/exactly-one(OrderID/data(.)))
    let $totalValue := 
       sum(
          for $detail in doc("orderDetails.xml")/OrderDetails[exactly-one(OrderID/data(.)) = $orderId]
          return $detail/Quantity * $detail/UnitPrice
       )
    return <order id="{$orderId}" totalValue="{$totalValue}" />
    

    This query then has the same semantics as a group-by and should be optimized as such. As it happens this still doesn’t optimize to a group by in XQSharp, so I have filed this as a bug against our software. Whether XmlSpy performs this optimization or not, I couldn’t say.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a simple function which will take a FileInfo and a destination_directory_name as
Can u give me a simple function in vb.net which can take number of
Take a simple example: I have a customer table and an account table both
Take a simple table like below: Column Headings: || Agent's Name || Time Logged
Update I'm trying to create a simple Go function which will simply take in
I'm looking for the best way to take a simple input: echo -n Enter
Take a very simple case as an example, say I have this URL: http://www.example.com/65167.html
The program needs to take in a simple string from the user and display
I find myself writing delegates occasionally for really simple functions (take no arguments and
Hopefully a simple question. Take for instance a Circularly-linked list: class ListContainer { private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.