Skip to content

18. All Combinations with CUBE

Difficulty: Hard · Topics: Pivot, Unpivot & Rollup, Aggregations, Conditional Logic

Build an OLAP-style summary of orders across channel and category: every combination, the total per channel, the total per category and the overall total.

Return channel, category, orders (row count) and revenue (sum). In rows that total over a dimension, show 'ALL' in that column. A category that is genuinely missing in the data must stay null.

Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.

Sample data

orders

channelcategoryrevenue
webbooks20
webtoys35
storebooks15
webbooks10

Expected output

channelcategoryordersrevenue
webbooks230
webtoys135
storebooks115
webALL365
storeALL115
ALLbooks345
ALLtoys135
ALLALL480

Hints

Hint 1df.cube("channel", "category") aggregates over all four grouping sets.
Hint 2F.grouping_id() packs the grouping bits into one number: here 0 = both columns present, 1 = category rolled up, 2 = channel rolled up, 3 = grand total.
Hint 3Replace a column with F.lit("ALL") only when its bit is set; don't use fillna, which would also overwrite real nulls.

PySpark functions you'll practise

Related problems

Browse

Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings

Difficulty: Easy · Medium · Hard · PySpark interview roadmap · All problems