[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Xquery total

Posted on 2006-03-22
2
Medium Priority
?
404 Views
Last Modified: 2006-11-18
I am trying to write a xquery to get the total tuition paid by a  student.  the tuition per course is represented
by cost in my xml file below. My xml file and query are below:  Thanks

<StudentRecord xmlns:StudentRecord="http://www.studentRecord.com/">
  <Students>
        <student id="1111">
            <Name>John Brown</Name>
            <Course>Calculus</Course>
            <Cost>100.00</Cost>
      </student>
        <student id="2222">
            <Name>John Doer</Name>
            <Course>English</Course>
            <Cost>50.00</Cost>
        </student>
        <student id = "1111">
           <Name>John Brown</Name>
            <Course>CalculusII</Course>
            <Cost>150.00</Cost>
        </student>

    </Students>
</StudentRecord>

Here is my Xquery:
<solution>
{for $i in distinct-values(//Students/student/@id)
        let $tuition := 0
        return <student StudentID ="{$i}">
                {for $j in //Students/student[@id=$i]
                let $tuition := $tuition+$j/Cost
                return <tuition> {$tuition}</tuition>
                }
</student>
}
</solution>


Here is my result:
<solution>
  <student StudentID="1111">
    <tuition>100</tuition>
    <tuition>150</tuition>
  </student>
  <student StudentID="2222"><tuition>50</tuition></student>
</solution>

Here is what I'd like instead:
<solution>
  <student StudentID="1111">
    <tuition>250</tuition>
  </student>
  <student StudentID="2222"><tuition>50</tuition></student>
</solution>

0
Comment
Question by:nebest
  • 2
2 Comments
 
LVL 12

Expert Comment

by:jkmyoung
ID: 16259218
               return <tuition> {$tuition}</tuition>
should be outside of the for $j statement.

Also, I think you're creating a new LOCAL variable $tuition each time you run
let $tuition := $tuition+$j/Cost

you'd need some sort of sum function or extension. Sorry that I'm not familiar enough with XQuery
0
 
LVL 12

Accepted Solution

by:
jkmyoung earned 500 total points
ID: 16259293
sorry, ignore my last comment: Was in a java state of mind.
You want something like:

let $tuition = sum(//Students/student[@id=$i]/Cost)

...
return <student StudentID ="{$i}">
           <tuition> {$tuition}</tuition>
         </student>

Forget the other for statement.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question