Based on the below sample data, we need to calculate a new column for each monthname and MonthProjectionNumber , the formula for the column is as follows:
Example for MonthName : Feb and MonthProjectionNumber : 2, the calculation formula is : sum of (Periodvalue of Feb,where MonthProjectionNumber = 1 + PeriodValueof Jan, where MonthProjectionNumber = 2)
Example for MonthName : Mar and MonthProjectionNumber : 3, the calculation formula is : sum of (Periodvalue of Mar,where MonthProjectionNumber = 1 + PeriodValueof Feb, where MonthProjectionnumber = 2 + MonthPeriodvalueofJan,where MonthProjectionNumber = 3)
rest of the MonthProjectionNumber values greater than the MonthNameNumber will be zero,example for month feb all monthprojectionnumber values > 2 will be zero
similarly for march, all projectionnumber> 3 will be zero,
Note : We are calculating monthprojectionnumber for that particular month
create table #test
(
YearNo int,
MonthName varchar(25),
UnitOfAccount Varchar(25),
MonthProjectionNumber int,
PeriodValue decimal(12,3)
)
INSERT INTO #TEST Values (2020, 'Jan' ,'Term-Onerous-2020', 1 ,250.754317962521)
INSERT INTO #TEST Values (2020, 'Jan' ,'Term-Onerous-2020', 2 ,350.754317962521)
INSERT INTO #TEST Values (2020, 'Jan' ,'Term-Onerous-2020', 3 ,280.752661975435)
INSERT INTO #TEST Values (2020, 'Jan' ,'Term-Onerous-2020', 4 ,375.753413961762)
INSERT INTO #TEST Values (2020, 'Jan' ,'Term-Onerous-2020', 5 ,260.753069962936)
INSERT INTO #TEST Values (2020, 'Jan' ,'Term-Onerous-2020', 6 ,300.752725964404)
INSERT INTO #TEST Values (2020, 'Feb' ,'Term-Onerous-2020', 1 ,0 )
INSERT INTO #TEST Values (2020, 'Feb' ,'Term-Onerous-2020', 2 ,200.754317962521)
INSERT INTO #TEST Values (2020, 'Feb' ,'Term-Onerous-2020', 3 ,200.752661975435)
INSERT INTO #TEST Values (2020, 'Feb' ,'Term-Onerous-2020', 4 ,200.753413961762)
INSERT INTO #TEST Values (2020, 'Feb' ,'Term-Onerous-2020', 5 ,200.753069962936)
INSERT INTO #TEST Values (2020, 'Feb' ,'Term-Onerous-2020', 6 ,200.752725964404)
INSERT INTO #TEST Values (2020, 'Mar' ,'Term-Onerous-2020', 1 ,0)
INSERT INTO #TEST Values (2020, 'Mar' ,'Term-Onerous-2020', 2 ,100.754317962521)
INSERT INTO #TEST Values (2020, 'Mar' ,'Term-Onerous-2020', 3 ,100.752661975435)
INSERT INTO #TEST Values (2020, 'Mar' ,'Term-Onerous-2020', 4 ,100.753413961762)
INSERT INTO #TEST Values (2020, 'Mar' ,'Term-Onerous-2020', 5 ,100.753069962936)
INSERT INTO #TEST Values (2020, 'Mar' ,'Term-Onerous-2020', 6 ,100.752725964404)
select * from #test