57. Subtotals with ROLLUP
Difficulty: Hard · Topics: Pivot, Unpivot & Rollup, Aggregations, Conditional Logic, Filtering & Selection
Finance wants one report with three levels of totals from the sales table: revenue per region and product, a subtotal per region, and a grand total.
Return region, product, total (sum of amount) and level, which is 'detail', 'region subtotal' or 'grand total'.
Careful: some sales have no product recorded (product is null). Those are still 'detail' rows; a null alone must not be mistaken for a subtotal.
Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.
Sample data
sales
| region | product | amount |
|---|---|---|
| North | Laptop | 1200 |
| North | Phone | 800 |
| North | Laptop | 300 |
| South | Phone | 500 |
| South | Tablet | 450 |
Expected output
| region | product | total | level |
|---|---|---|---|
| North | Laptop | 1500 | detail |
| North | Phone | 800 | detail |
| South | Phone | 500 | detail |
| South | Tablet | 450 | detail |
| North | null | 2300 | region subtotal |
| South | null | 950 | region subtotal |
| null | null | 3250 | grand total |
Hints
Hint 1
df.rollup("region", "product") produces the (region, product), (region) and () groupings in one pass.Hint 2
In subtotal rows the rolled-up columns arenull. F.grouping("product") returns 1 only when the null comes from the rollup, not from the data.Hint 3
Computegrouping() inside .agg(...), then build level with F.when.Learn the concepts
- rollup / cube · 4 min read. Subtotals and grand totals in one pass, and how grouping() tells a total row from a real null.
- when / otherwise · 4 min read. CASE WHEN logic inside a column: first match wins, and a missing otherwise means null.
- select · 6 min read. Choose, rename and compute columns, and why select beats a chain of withColumn calls.
PySpark functions you'll practise
- agg
- sum
- rollup
- grouping
- when / otherwise
- select
- withColumn
Related problems
- All Combinations with CUBE · Hard · Pivot, Unpivot & Rollup, Aggregations, Conditional Logic
- Pivot with Several Aggregations · Hard · Pivot, Unpivot & Rollup, Aggregations, Null Handling
- Pivot Quarterly Revenue · Medium · Pivot, Unpivot & Rollup, Aggregations
- Survey Scores by Question · Medium · Pivot, Unpivot & Rollup, Aggregations
- Unpivot Wide Columns to Rows · Medium · Pivot, Unpivot & Rollup, Null Handling, Filtering & Selection
Browse
Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance
Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice