Fix the lifetime revenue report per customer
Your task
An AI's SQL report joined both orders and refunds to customers in one query. A customer with 10 orders and 4 refunds became 40 rows, so revenue came out 4 times too high. The new version totals each table on its own and only then attaches the results to the customer.
An assistant wrote customer_revenue_report(customer_ids, order_rows, refund_rows). It is in the editor. It has a bug. Find it and fix it.
Input and output:
customer_ids: the customers to report on. An id can be listed more than once.order_rows: one order per row, writtencustomer_id,amount, with the amount in cents.refund_rows: one refund per row, writtencustomer_id,refund_id. The same refund can appear in more than one row.- Return one
customer_id:revenue:refund_countstring per customer.
Rules the tests check:
- Report each distinct customer id once, sorted as plain strings. A customer with no orders or refunds still appears, with 0 for both.
- Split each order row on
,. Skip it unless it has exactly 2 parts, its customer is incustomer_ids, and its amount is an optional-followed by one or more digits from 0 to 9. A customer's revenue is the sum of their amounts. - Split each refund row on
,. Skip it unless it has exactly 2 parts and its customer is incustomer_ids. A customer's refund count is the number of different refund ids they have: the same refund id listed twice counts once.
Examples
customer_revenue_report(customer_ids: list[str], order_rows: list[str], refund_rows: list[str]) -> list[str]Example 1
Input
customer_revenue_report(["c1", "c2"], ["c1,100", "c1,200", "c2,50"], ["c1,r1", "c1,r2"])Output
["c1:300:2", "c2:50:0"]Example 2
Input
customer_revenue_report(["c9"], ["c9,10", "c9,10", "c9,10", "c9,10", "c9,10", "c9,10", "c9,10", "c9,10", "c9,10", "c9,10"], ["c9,r1", "c9,r2", "c9,r3", "c9,r4"])Output
["c9:100:4"]Example 3
Input
customer_revenue_report([], ["c1,100"], [])Output
[]