I am very new to this site, to all. And I did not know how to ask. So I asked 1 problem earlier (I sent it as an answer), and I posted the solution again, just like me. All your ideas helped me solve this problem.
Now a new problem has appeared.
I want to build columns that compute with each other. (Sorry for my English) Example:
Id Column1 Column2 Column3
1 5 5 => Same as Column1 5 => Same as Column2
2 2 12 => column1 current + column2.prev + column3.previous = 2+5+5 17 => column2.current + column3.prev = 12+5
3 3 32 => 3+12+17 49 => 32+17
easier way to see:
Id Column1 Column2 Column3
1 5 5 => Same as Column1 5 => Same as Column2
2 2 12 => 2+5+5 17 => 12+5
3 3 32 => 3+12+17 49 => 32+17
so difficult???: - (
The previous problem was calculating column 3 with the new calculated column as with column2. But now it should be updated with the just calculated Column2 column and the previous Column3 record. If you want to see the previous post, here it is .
, .
, , , .
, .
CTE. 1-, 2 (c.Column2) cteCalculation, 3 cte2 2 cteCalculation.
/ /
;with cteCalculation as (
select t.Id, t.Column1, t.Column1 as Column2
from table_1 t
where t.Id = 1
union all
select t.Id, t.Column1, (t.Column1 + c.Column2) as Column2
from table_1 t
inner join cteCalculation c
on t.Id-1 = c.id
),
cte2 as(
select t.Id, t.Column1 as Column3
from table_1 t
where t.Id = 1
union all
select t.Id, (select column2+1 from cteCalculation c where c.id = t.id) as Column3
from table_1 t
inner join cte2 c2
on t.Id-1 = c2.id
)
select c.Id, c.Column1, c.Column2, c2.column3
from cteCalculation c
inner join cte2 c2 on c.id = c2. id
, 2 . , 2- 3- 3- 2- . , .