Xquery total

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>

nebestAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkmyoungCommented:
               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
jkmyoungCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.