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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...