Skip to content
Advertisement

Keep the sum even on days without revenue in cumulative sum when using window function in Presto

So my problem is that I have sales data from, say for the sake of clarity, 3 different products. I will be selling 10 of these a week and I want to visualize them in a cumulative sum. I have been using the following little snippet to get the cumulative sum of the revenue.

However, this is not sufficient since I have the product in the window function. It works only as long as I have sales data on every single day of the week for each product. If I sell product_1 four pieces a week, the result from the query will show cumulative revenue only from those days. So this means that if I have all sales of that product on Monday to Wednesday, the rest of the week won’t have them listed in the output. This causes problems if I try to visualize the data by stacking the results as the rest of the week will have lower cumulative revenue that the beginning of the week.

So what I want is to get it to show 0 on revenue for all products on all days of the week. I can of course do this with some cross join magic, but it is sloooow since I have quite a lot of rows, so is there a way to do it with a window function?

My data looks a bit like this

Advertisement

Answer

You can generate a row for all product/date combinations by using a cross join and then a left join. I suspect you want something like this:

Note: This assumes that there is at least one purchase on each day. Otherwise, you might need another source for all dates in the range you care about.

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