CODE WORKSPACE

SCD2 change boundaries

Return buyer_id, paid_revenue (sum of total_amount where status is paid), and refunded_amount (sum of total_amount where status is refunded) for each buyer. Order by buyer_id ascending.

GROUP BY buyer_id; SUM(CASE WHEN status = 'paid' THEN total_amount ELSE 0 END) AS paid_revenue, and the same for refunded.

Sample input

ordersOnly buyer 6 has a refunded order (1006, $60). Shipped/pending orders contribute 0.
order_idbuyer_idstatustotal_amount
10011paid80
10052paid540
10112paid110
10106paid640
10066refunded60

Expected output

Expected outputPaid revenue and refunded amount per buyer in one row. Buyer 4 has neither, so both are 0.
buyer_idpaid_revenuerefunded_amount
1800
26500
32150
400
664060

Constraints

GROUP BY buyer_id. Use SUM(CASE WHEN status = 'paid' THEN total_amount ELSE 0 END) and the same pattern for refunded. Every buyer appears once, with 0 where a status is absent.

Expected skills

Conditional aggregation: a CASE inside SUM to compute several metrics in one pass.

SQL
Loading...

AI evaluation

Run the SQL query to inspect preview rows.