Medium · Database

Monthly Payment Totals

Given Payments(id, month, status, amount), return Month, PaidTotal, and RefundedTotal for every month present. The month is YYYY-MM; status is paid, refunded, or pending. Pending payments contribute zero; row order does not matter.

Examples

Example 1

{
  "payments": [
    {"id": 1, "month": "2026-01", "status": "paid", "amount": 100},
    {"id": 2, "month": "2026-01", "status": "paid", "amount": 20},
    {"id": 3, "month": "2026-01", "status": "refunded", "amount": 30},
    {"id": 4, "month": "2026-02", "status": "paid", "amount": 20},
    {"id": 5, "month": "2026-02", "status": "pending", "amount": 50}
  ]
}

Output: [{"Month":"2026-01","PaidTotal":120,"RefundedTotal":30},{"Month":"2026-02","PaidTotal":20,"RefundedTotal":0}]

Example 2

{
  "payments": [
    {"id": 6, "month": "2026-03", "status": "pending", "amount": 70}
  ]
}

Output: [{"Month":"2026-03","PaidTotal":0,"RefundedTotal":0}]

Rebuild it in the studio

20 free core interview problems with full runner & lessons; explore the complete 380+ library with Pro.

More Database problems