I need to calculate and update the YTD (YTDDepr column) for each item for each day in a history table. But I only have the MTD values, and sometimes there can be adjustments at month end. I have a calendar table which gives the Financial Year Number (FYN) for every date (our year runs April to March).
The history table for 2 example items looks like this:
+--------+------------+---------+--------+
| ItemNo | StatusDate | MthDepr | YTDDepr |
+--------+------------+---------+--------+
| 88190 | 28/02/2022 | 23.24 | 0 |
| 88191 | 28/02/2022 | 77.84 | 0 |
| 88190 | 01/03/2022 | 0.83 | 0 |
| 88191 | 01/03/2022 | 2.78 | 0 |
| 88190 | 02/03/2022 | 1.66 | 0 |
| 88191 | 02/03/2022 | 5.56 | 0 |
| 88190 | 03/03/2022 | 2.49 | 0 |
| 88191 | 03/03/2022 | 8.34 | 0 |
+--------+------------+---------+--------+
The calendar table looks like this:
+-----+------------+------+
| SID | CalDate | FYN |
+-----+------------+------+
| 547 | 24/02/2022 | 2022 |
| 557 | 25/02/2022 | 2022 |
| 567 | 26/02/2022 | 2022 |
| 577 | 27/02/2022 | 2022 |
| 587 | 28/02/2022 | 2022 |
| 597 | 01/03/2022 | 2022 |
| 607 | 02/03/2022 | 2022 |
| 617 | 03/03/2022 | 2022 |
| 627 | 04/03/2022 | 2022 |
| 637 | 05/03/2022 | 2022 |
| 647 | 06/03/2022 | 2022 |
+-----+------------+------+
The result for the 2 examples should look like this:
+--------+------------+---------+--------+
| ItemNo | StatusDate | MthDepr | YTDDepr |
+--------+------------+---------+--------+
| 88190 | 28/02/2022 | 23.24 | 255.64 |
| 88191 | 28/02/2022 | 77.84 | 856.24 |
| 88190 | 01/03/2022 | 0.83 | 256.47 |
| 88191 | 01/03/2022 | 2.78 | 859.02 |
| 88190 | 02/03/2022 | 1.66 | 257.30 |
| 88191 | 02/03/2022 | 5.56 | 861.80 |
| 88190 | 03/03/2022 | 2.49 | 258.13 |
| 88191 | 03/03/2022 | 8.34 | 864.58 |
+--------+------------+---------+--------+
What is the easiest way to get and set the amount for the YTDDepr column?
Many thanks
Martyn