I have a table.
SELECT * INTO #tmp
FROM (
SELECT 1 AS [ID], 20200312 AS [date], 0 AS [ValueA], 0.00 AS [ValueB], 480.00 AS [ValueC], 4906 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200313 AS [date], 0 AS [ValueA], 0.00 AS [ValueB], 1440.00 AS [ValueC], 3466 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200314 AS [date], 0 AS [ValueA], 1000.00 AS [ValueB], 0.00 AS [ValueC], 4466 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200318 AS [date], 0 AS [ValueA], 0.00 AS [ValueB], 1056.00 AS [ValueC], 3410 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200319 AS [date], 0 AS [ValueA], 0.00 AS [ValueB], 864.00 AS [ValueC], 2546 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200320 AS [date], 0 AS [ValueA], 0.00 AS [ValueB], 1296.00 AS [ValueC], 1250 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200321 AS [date], 0 AS [ValueA], 4000.00 AS [ValueB], 624.00 AS [ValueC], 4626 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200324 AS [date], 0 AS [ValueA], 0.00 AS [ValueB], 1152.00 AS [ValueC], 3474 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200325 AS [date], 3474 AS [ValueA], 0.00 AS [ValueB], 2718.00 AS [ValueC], 756 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200330 AS [date], 0 AS [ValueA], 6000.00 AS [ValueB], 1080.00 AS [ValueC], 5676 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200401 AS [date], 0 AS [ValueA], 0.00 AS [ValueB], 1920.00 AS [ValueC], 2756 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200403 AS [date], 0 AS [ValueA], 0.00 AS [ValueB], 1920.00 AS [ValueC], 836 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200407 AS [date], 0 AS [ValueA], 3000.00 AS [ValueB], 0.00 AS [ValueC], 3836 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200408 AS [date], 0 AS [ValueA], 0.00 AS [ValueB], 2448.00 AS [ValueC], 1388 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200413 AS [date], 0 AS [ValueA], 4000.00 AS [ValueB], 0.00 AS [ValueC], 5388 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200415 AS [date], 0 AS [ValueA], 0.00 AS [ValueB], 1920.00 AS [ValueC], 3468 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200417 AS [date], 0 AS [ValueA], 0.00 AS [ValueB], 1920.00 AS [ValueC], 1548 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200420 AS [date], 0 AS [ValueA], 1000.00 AS [ValueB], 1920.00 AS [ValueC], 628 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200426 AS [date], 0 AS [ValueA], 4000.00 AS [ValueB], 0.00 AS [ValueC], 4628 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200515 AS [date], 0 AS [ValueA], 0.00 AS [ValueB], 3840.00 AS [ValueC], 788 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200525 AS [date], 0 AS [ValueA], 3000.00 AS [ValueB], 1920.00 AS [ValueC], 1868 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200601 AS [date], 0 AS [ValueA], 2000.00 AS [ValueB], 1080.00 AS [ValueC], 2788 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200608 AS [date], 0 AS [ValueA], 1000.00 AS [ValueB], 1920.00 AS [ValueC], 1868 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200615 AS [date], 0 AS [ValueA], 2000.00 AS [ValueB], 0.00 AS [ValueC], 3868 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200622 AS [date], 0 AS [ValueA], 0.00 AS [ValueB], 1920.00 AS [ValueC], 1948 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200706 AS [date], 0 AS [ValueA], 2000.00 AS [ValueB], 1920.00 AS [ValueC], 2028 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200713 AS [date], 0 AS [ValueA], 2000.00 AS [ValueB], 0.00 AS [ValueC], 4028 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200720 AS [date], 0 AS [ValueA], 0.00 AS [ValueB], 3000.00 AS [ValueC], 1028 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200727 AS [date], 0 AS [ValueA], 3000.00 AS [ValueB], 0.00 AS [ValueC], 4028 AS [NewColumn] UNION ALL
SELECT 1 AS [ID], 20200803 AS [date], 0 AS [ValueA], 0.00 AS [ValueB], 1920.00 AS [ValueC], 2108 AS [NewColumn]
) t;
[NewColumn] is the desired output
SELECT [ID], [date], [ValueA], [ValueB], [ValueC], [NewColumn]
FROM #tmp
order by [date]
Based on values for columns A,B and C the only value that i can calculate is where the date is 20200325 and is calculated based on formula below.
update #tmp
set [NewColumn] = ValueA+ValueB-ValueC
where date = 20200325
So the value is 756
all others rows are calculated based on the previous row ex:
[NewColumn](for date 20200330) = [NewColumn](for date 20200325)+ValueA+ValueB-ValueC
X = 756 + 0 + 6000 - 1080
X = 5676
and so on...
Is there a way to achive this in sql through an update statement
PS. I need to update before 20200325 as well as after that date
Assuming that you are runnin SQL-Server, as the syntax suggests, you could use an updateble cte and window functions:
Demo on DB Fiddle: