Skip to content

11. Find Repeat Customers

Difficulty: Easy · Topics: Aggregations, Filtering & Selection

orders has one row per order line: order_id, customer_id, product and amount. An order can have several lines.

For every customer with at least 2 different orders, return customer_id, orders (distinct order ids), products (distinct products) and total (sum of amount).

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

Sample data

orders

order_idcustomer_idproductamount
1c1pen10
1c1ink5
2c1pen10
3c2book300
4c3pen10
5c3pen10

Expected output

customer_idordersproductstotal
c12225
c32120

Hints

Hint 1F.countDistinct("order_id") counts orders, not lines.
Hint 2Aggregate first, then filter on the aggregated column (SQL's HAVING).
Hint 3Several aggregates can go in one agg call.

Learn the concepts

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 · Learn · All problems