WoodleySaint Posted March 2, 2018 Share 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 Link to comment Share on other sites More sharing options...
Ingolme Posted March 2, 2018 Share 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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now