I have this table1:
| date | typeName | Typecode | Values1 | Values2 | Values3 |
|---|---|---|---|---|---|
| 2022-09-01 | A | 01 | null | 5 | null |
| 2022-09-02 | A | 01 | null | 10 | null |
| 2022-09-01 | B | 02 | null | 30 | null |
| 2022-09-02 | B | 02 | null | 10 | null |
And the table2:
| date | typeName | Typecode | Values1 |
|---|---|---|---|
| 2022-09-01 | A | 01 | 40 |
| 2022-09-01 | B | 02 | 50 |
I want to update table1 using the data from table2 such that
values3 = values1 - values2 and next day , values3 of previous date = values 1 or next day and so on.
| date | typeName | Typecode | Values1 | Values2 | Values3 |
|---|---|---|---|---|---|
| 2022-09-01 | A | 01 | 40 | 5 | 35 |
| 2022-09-02 | A | 01 | 35 | 10 | 25 |
| 2022-09-01 | B | 02 | 50 | 30 | 20 |
| 2022-09-02 | B | 02 | 20 | 10 | 10 |
How to write the T-SQL to do that?