Of course you can make this as a function!But the sample/test data you have provided is horrible, and you have made roundings in some cases and not in other cases.However, this does the trick for you.set dateformat mdy-- prepare sample datacreate table benefit (benefitinterestid int, benefittypeid int, startdate datetime, enddate datetime, interest decimal(5, 2))insert benefitselect 4, 3, '12/01/2006', '11/30/2007', .02 union allselect 3, 3, '12/01/2005', '11/30/2006', .03 union allselect 2, 3, '12/01/2004', '11/30/2005', .04 union allselect 1, 3, '12/01/2003', '11/30/2004', .05create table interest (benefittypeid int, receiveddate datetime, amount money, interest decimal(5, 2), interestamount money)insert interestselect 3, '12/11/2003', 100.00, .05, 4.583 union allselect 3, '01/11/2004', 100.00, .05, 4.116 union allselect 3, '02/11/2004', 100.00, .05, 3.75 union allselect 3, '03/11/2004', 100.00, .05, 3.333 union allselect 3, '04/11/2004', 100.00, .05, 2.916 union allselect 3, '05/11/2004', 100.00, .05, 2.5 union allselect 3, '06/11/2004', 100.00, .05, 2.083 union allselect 3, '07/11/2004', 100.00, .05, 1.666 union allselect 3, '08/11/2004', 100.00, .05, 1.25 union allselect 3, '09/11/2004', 100.00, .05, 0.833 union allselect 3, '10/11/2004', 100.00, .05, 0.416 union allselect 3, '11/11/2004', 100.00, .05, 0 union allselect 3, '12/11/2004', 100.00, .04, 3.666 union allselect 3, '01/11/2005', 100.00, .04, 3.333 union allselect 3, '02/11/2005', 100.00, .04, 3 union allselect 3, '03/11/2005', 100.00, .04, 2.666 union allselect 3, '04/11/2005', 100.00, .04, 2.333 union allselect 3, '05/11/2005', 100.00, .04, 2 union allselect 3, '06/11/2005', 100.00, .04, 1.666 union allselect 3, '07/11/2005', 100.00, .04, 1.333 union allselect 3, '08/11/2005', 100.00, .04, 1 union allselect 3, '09/11/2005', 100.00, .04, 0.666 union allselect 3, '10/11/2005', 100.00, .04, 0.333 union allselect 3, '11/11/2005', 100.00, .04, 0 union allselect 3, '12/11/2005', 100.00, .03, 2.75 union allselect 3, '01/11/2006', 100.00, .03, 2.5 union allselect 3, '02/11/2006', 100.00, .03, 2.25 union allselect 3, '03/11/2006', 100.00, .03, 2 union allselect 3, '04/11/2006', 100.00, .03, 1.75 union allselect 3, '05/11/2006', 100.00, .03, 1.5 union allselect 3, '06/11/2006', 100.00, .03, 1.25 union allselect 3, '07/11/2006', 100.00, .03, 1 union allselect 3, '08/11/2006', 100.00, .03, 0.75 union allselect 3, '09/11/2006', 100.00, .03, 0.5 union allselect 3, '10/11/2006', 100.00, .03, 0.25 union allselect 3, '11/11/2006', 100.00, .03, 0 union allselect 3, '12/11/2006', 100.00, .02, 1.8333 union allselect 3, '01/11/2007', 100.00, .02, 1.666 union allselect 3, '02/11/2007', 100.00, .02, 1.5 union allselect 3, '03/11/2007', 100.00, .02, 1.333 union allselect 3, '04/11/2007', 100.00, .02, 1.166 union allselect 3, '05/11/2007', 100.00, .02, 1 union allselect 3, '06/11/2007', 100.00, .02, 0.833 union allselect 3, '07/11/2007', 100.00, .02, 0.666 union allselect 3, '08/11/2007', 100.00, .02, 0.5 union allselect 3, '09/11/2007', 100.00, .02, 0.333 union allselect 3, '10/11/2007', 100.00, .02, 0.166 union allselect 3, '11/11/2007', 100.00, .02, 0goCREATE FUNCTION dbo.fnGetMyInterest( @EmployeeID INT)RETURNS MONEYASBEGIN DECLARE @Stage TABLE (BenefitInterestID INT PRIMARY KEY, Amount MONEY, InterestAmount MONEY, Interest DECIMAL(5, 2), ai DECIMAL(5, 2)) INSERT @Stage SELECT b.BenefitInterestID, SUM(i.Amount) AS Amount, SUM(i.InterestAmount) AS InterestAmount, 0, 0 FROM Benefit AS b INNER JOIN Interest AS i ON i.ReceivedDate >= b.StartDate AND i.ReceivedDate <= b.EndDate-- WHERE i.EmployeeID = @EmployeeID GROUP BY b.BenefitInterestID ORDER BY b.BenefitInterestID UPDATE s SET s.Interest = (SELECT b.Interest FROM Benefit AS b WHERE b.BenefitInterestID = s.BenefitInterestID + 1) FROM @Stage AS s UPDATE s1 SET s1.ai = s1.Interest * (SELECT SUM(s2.Amount + s2.InterestAmount) FROM @Stage AS s2 WHERE s2.BenefitInterestID <= s1.BenefitInterestID) FROM @Stage AS s1 DECLARE @Result MONEY SELECT @Result = SUM(Amount) + SUM(InterestAmount) + SUM(ai) FROM @Stage RETURN @ResultENDgo-- show the resultselect dbo.fnGetMyInterest(1)-- clean updrop table benefitdrop table interestdrop function fnGetMyInterest
Peter LarssonHelsingborg, Sweden