I trying to perform following calculation
Sample data:
CREATE TABLE #Table1
(
rno int identity(1,1),
ccp varchar(50),
Another option
CREATE TABLE #Table1
(
rno int identity(1,1),
ccp varchar(50),
[col1] INT,
[col2] INT,
[col3] INT,
col4 as [col2]/100.0
);
INSERT INTO #Table1
(ccp,[col1],[col2],[col3])
VALUES ('ccp1',15,10,1100),
('ccp1',20,10,1210),
('ccp1',30,10,1331),
('ccp1',40,10,1331),
('ccp2',10,15,900),
('ccp2',15,15,1000),
('ccp2',20,15,1010);
select t.*, col3-s
from(
select *, rn = row_number() over(partition by ccp order by rno)
from #Table1
) t
cross apply (
select s=sum(pwr*col1)
from(
select top(rn)
col1, pwr = power(1+col4, rn + 1 - row_number() over(order by rno))
from #Table1 t2
where t2.ccp=t.ccp
order by row_number() over(order by rno)
)t3
)t4
order by rno;