Skip to content
Advertisement

How to query cumulative sum period by month

I have an issue with table source like this:

enter image description here

Available month in table is only as is from column month, no April, May, June etc.

I need to create a view to create “Year to Date” table as shown here:

enter image description here

I need to view all months from Year 2020 January – December with value based on table source.

VALUE COLUMN filled by Source with same month, but because April not available from source, so it automatically gets set to 0 and others are same

For CUMULATIVE VALUE COLUMN filled by value Sum from last month value

I’ve tried using case when query but it not simple if I have to many Name and not dynamic

Advertisement

Answer

You can use next SQL construction:

MSSQL cumulative sum

The query use CTE expression for generate calendar table and window function SUM over partition by name to calculate cumulative values

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement