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 7632853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:43:52+00:00 2026-05-31T06:43:52+00:00

I have an SQL query: SELECT ShipVia, SUM(Freight) FROM Orders GROUP BY ShipVia which

  • 0

I have an SQL query:

SELECT ShipVia, SUM(Freight)
FROM Orders
GROUP BY ShipVia

which returns the values below from the access database

Ship Via  TotalFreight
   1       $16,185.33
   2       $28,244.85
   3       $20,512.51

I am trying to convert this to xquery (using xquery 1.0)

So far I have this,

xquery version "1.0";
for $x in doc("Orders.xml")/dataroot/Orders
return
<OrderDetails>
{
    $x/ShipVia,
    <TotalFreight>{sum($x/Freight)}</TotalFreight>
}
</OrderDetails>

This however, outputs every single order along with the freight cost as if I did

SELECT ShipVia, Freight
FROM Orders

in SQL

How do I make the xquery actually add each one up as the SQL command does

These are three orders from the Orders.xml file for example

<dataroot>
    <Orders>
        <ShipVia>1</ShipVia>
        <Freight>32.38</Freight>
    </Orders>
    <Orders>
        <ShipVia>1</ShipVia>
        <Freight>11.61</Freight>
    </Orders>
    <Orders>
        <ShipVia>2</ShipVia>
        <Freight>65.83</Freight>
    </Orders>
</dataroot>

EDIT: Stripped Unnecessary Nodes (Added Duplicate)

  • 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-31T06:43:53+00:00Added an answer on May 31, 2026 at 6:43 am

    The following is one approach to doing this in XQuery 1.0 (which lacks XQuery 3.0’s native group by operator):

    let $doc := 
        <dataroot>
            <Orders>
                <ShipVia>1</ShipVia>
                <Freight>32.38</Freight>
            </Orders>
            <Orders>
                <ShipVia>1</ShipVia>
                <Freight>11.61</Freight>
            </Orders>
            <Orders>
                <ShipVia>2</ShipVia>
                <Freight>65.83</Freight>
            </Orders>
        </dataroot>
    
    let $ship_via_values := distinct-values($doc/Orders/ShipVia/text())
    for $ship_via_value in $ship_via_values
    return
      <OrderDetails>
        <ShipVia>{$ship_via_value}</ShipVia>
        <TotalFreight>{
          sum($doc/Orders[ShipVia=$ship_via_value]/Freight)
        }</TotalFreight>
      </OrderDetails>
    

    This query returns the following result:

    <OrderDetails>
      <ShipVia>1</ShipVia>
      <TotalFreight>43.99</TotalFreight>
    </OrderDetails>
    <OrderDetails>
      <ShipVia>2</ShipVia>
      <TotalFreight>65.83</TotalFreight>
    </OrderDetails>
    

    …which appears to be desired output.

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

Sidebar

Related Questions

I have SQL SELECT query which returns a lot of rows, and I have
I have an SQL query : SELECT Sum(ABS([Minimum Installment])) AS SumOfMonthlyPayments FROM tblAccount INNER
I have folowing sql query select sta_name Station,count(factory_name) as factories from gps1 group by
I have below SQL query: SELECT * FROM ( SELECT S.aCol, S.bCol, S.cCol, ROW_NUMBER()
I have a SQL query which retrieve wall post list $qry=mysql_query(SELECT senderid,post,date FROM .post_table.
I have sql query like this: select a.x, a.y, sum(a.foo) as foo_sum from a
i have sql query select * from Roles Join Users On Roles.Role=Users.RoleId it return
I have this SQL query: SELECT * FROM IMAGES WHERE IMAGENAME in ('IMG1', 'IMG2',
I have the following SQL query: select expr1, operator, expr2, count(*) as c from
I have a following SQL QUERY: SELECT articles.name, articles.price, users.zipcode FROM articles INNER JOIN

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.