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

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
take this simple code: class A{ public: virtual void foo() = 0; void x(){
Take a simple XML file formatted like this: <Lists> <List> <Note/> ... <Note/> </List>
Take this simple query: SELECT DATE_FORMAT(someDate, '%y-%m-%d') as formattedDay FROM someTable GROUP BY formatterDay
Take this simple, contrived example: UserRepository.GetAllUsers(); UserRepository.GetUserById(); Inevitably, I will have more complex queries,
Take a simple class with the big 3 (constructor, copy constructor, destructor): #include <vector>
I have a VB.Net function which executes a Oracle Stored Proc and returns a
This is a pretty odd situation I have here. I have used a piece

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.