Home > Software design >  Cumulative sum reset by change of year (SQL Server)
Cumulative sum reset by change of year (SQL Server)

Time:05-31

I'm using SQL Server 2017. I would like to sum up the budget per month of a year for that year and factory.

This cumulation is to be reset with each new year.

Table schema:

CREATE TABLE [TABLE_1] 
(
    FACTORY varchar(50) Null,
    DATE_YM int Null,
    BUDGET int NULL,
);

INSERT INTO TABLE_1 (FACTORY, DATE_YM, BUDGET)
VALUES ('A', 202111, 1),
       ('A', 202112, 1),
       ('A', 202201, 10),
       ('A', 202202, 100),
       ('A', 202203, 1000),
       ('B', 202111, 2),
       ('B', 202112, 2),
       ('B', 202201, 20),
       ('B', 202202, 200),
       ('B', 202203, 2000),
       ('C', 202111, 3),
       ('C', 202112, 3),
       ('C', 202201, 30),
       ('C', 202202, 300),
       ('C', 202203, 3000);

enter image description here

  • Related