Calculating cumulative sales with windowing functions


Suppose you are given the following table showing company sales:

Table: sales_info

date sale_id sale_usd
2020-01-05 1111 93695
2020-01-07 1112 879617
2020-01-07 1113 752878
... ... ...

Calculate the cumulative percent of total sales on a given day. Cumulative sales up to a day is calculated adding the total sales of the current day and adding all the prior day total sales. The total sales can be calculated by adding all sales that exist in the table. To get the percent of cumulative total sales, you will need to calculate the cumulative sales and divide by the total sales. The output of the table should look like the example table below.

date pct_total_sales
2020-01-05 X.XX
2020-01-07 Y.YY

You can view/query the data in an interactive SQL fiddle here.


Access restricted

Subscribe to premium account to see the solution.

Get premium now