Premium problem68. Net Revenue After Refunds

Easy Locked

payments(payment_id, order_id, payment_type, amount, created_at)

For each month of 2026 that has payments, return gross charges, refunds and net revenue. payment_type is 'charge' or 'refund', and refund amounts are stored as positive numbers.

Return month as 'YYYY-MM', gross_charges, refunds and net_revenue, ordered by month. A month with charges but no refunds shows 0 for refunds, not NULL.

Refunds are dated by when the refund happened, not by the month of the original charge, so a January charge refunded in February reduces February.

Tables

payments

payment_id  order_id  payment_type  amount  created_at
----------  --------  ------------  ------  ----------
1           100       charge        500.00  2026-01-10
2           101       charge        100.00  2026-01-20
3           100       refund        500.00  2026-02-02
4           102       charge        50.00   2026-02-03
5           103       charge        70.00   2026-03-01
6           104       charge        999.00  2025-12-31

Expected result

month    gross_charges  refunds  net_revenue
-------  -------------  -------  -----------
2026-01  600.00         0.00     600.00
2026-02  50.00          500.00   -450.00
2026-03  70.00          0.00     70.00

Premium problem

This one's part of Premium. Unlock the full MySQL track plus every other premium problem on the site.

Write one SELECT query