Jump to content

Sum of iterative rows


Recommended Posts


Here is a subset of some rows from a SQL table:


I would like to construct a select statement which will have a row for each record it finds where the CreditType is 0 and then sum records where the ParentFeeFee is the same as the FeeRef. So the orange ones would form one summation and the green another and the results would be:


The easy part is:

Select FeeRef from tableName where CreditType = 0

How would I perform the summation part?

Many thanks for any guidance.


Edited by WoodleySaint
Additional example not required
Link to comment
Share on other sites

I don't have time to verify that this actually will solve the problem at the moment, but even if it's wrong it might help you get on track to finding the correct solution.

SELECT t1.FeeRef, SUM(t2.Amount) AS Summation FROM table AS t1
LEFT JOIN table AS t2 ON t2.ParentFeeRef = t1.FeeRef
WHERE t1.CreditType = 0
GROUP BY t2.ParentFeeRef

Edit: Well, this will actually only work for one level of nesting. For an unspecified number of levels of nesting it's going to be somewhat more complicated, it might involve subqueries.

Edited by Ingolme
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...