Region Running Revenue

Medium
Window Functions

Region Running Revenue

The sales table records daily sales for each region.

Return region, sale_day, sale_id, amount, region_running_total, where region_running_total accumulates sales within each region from the earliest day through the current row. Sort by region, then sale_day, then sale_id ascending.

Browser Engine: Evaluates PySpark DataFrame & SQL logic locally in Pyodide WebAssembly.

Input data

1 DataFrame

These DataFrames are passed to solve(spark, dfs). Access one with dfs['name'].

df.sales7 rows
sale_id (int)region (string)sale_day (int)amount (double)
1East1100
2West180
3East250
4West220
5East330
6West370
7East410

Return these columns

region(string)
sale_day(int)
sale_id(int)
amount(double)
region_running_total(double)

Column names and order must match. Sort order is specified in the question.

Browser engine
Loading Python editor…
Click Run to try the sample cases, or Submit to check the hidden cases. First run caches the browser Python engine (~8–15s once); all code stays on your device.