WoodleySaint 0 Report post Posted March 2, 2018 (edited) Hi, 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 March 2, 2018 by WoodleySaint Additional example not required Share this post Link to post Share on other sites
Ingolme 939 Report post Posted March 2, 2018 (edited) 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 March 2, 2018 by Ingolme Share this post Link to post Share on other sites