Reputation: 11
Cam anyone help me ?
I want to Add the value 10 (ID-1, Column_2) to the 150 (ID-2, column_1): such us: 10 +150 = 160
That process should repeat for every next row.
So the next will be 5+ 130 = 135.
Using SQL Select statement, can someone help me to write that code ?
Thanks.
Upvotes: 1
Views: 60
Reputation: 1484
in sql server you can use inner join
Create table #tmp(ID int,column_1 int, column_2 int)
insert into #tmp values(1,100,10)
insert into #tmp values(2,150,5)
insert into #tmp values(3,130,20)
select * from #tmp
select t1.column_2+t2.column_1 from #tmp t1
INNER JOIN #tmp t2 on t1.Id=t2.Id-1
DROP table #tmp
Upvotes: 1
Reputation: 1269503
In SQL Server, you would use lag()
:
select col1 + lag(col2, 1, 0) over (order by id) as col1, col2
from t;
This assumes that you do not want to change the first value.
Upvotes: 3