Skip to content
Advertisement

SQL Cumulative Sum not showing when group by values are missing

I have some monthly data as below:

I want to calculate the cumulative sum for each Category and get the result as below:

I’m writing the window sum query as below:

But, I’m getting as follows:

Why is “Food” Category’s Cumulative Sum not showing up for the months of 2020-08-01 and 2020-09-01? How can I make the result be displayed as expected (shown in the 2nd table).

I’m using Redshift btw. Thanks!

Advertisement

Answer

Use a cross join to generate the rows and then left join to bring in the values:

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