Loading problem...
A finance analytics team needs a product-level reconciliation report from invoice events.
Table: product
Table: invoice
Task: For every product, return total rest, paid, canceled, and refunded amounts across all invoice rows linked to that product.
Business requirements:
Output requirements:
Supported submission environments:
product:
| product_id | name |
|------------|-------|
| 1 | alpha |
| 2 | beta |
invoice:
| invoice_id | product_id | rest | paid | canceled | refunded |
|------------|------------|------|------|----------|----------|
| 101 | 1 | 2 | 5 | 0 | 1 |
| 102 | 1 | 1 | 0 | 4 | 0 |
| 103 | 2 | 0 | 7 | 1 | 2 |[
{"name":"alpha","rest":3,"paid":5,"canceled":4,"refunded":1},
{"name":"beta","rest":0,"paid":7,"canceled":1,"refunded":2}
]Each amount column is summed independently per product. alpha combines two invoices, while beta combines one.
product:
| product_id | name |
|------------|--------|
| 10 | item-a |
| 20 | item-b |
| 30 | item-c |
invoice:
| invoice_id | product_id | rest | paid | canceled | refunded |
|------------|------------|------|------|----------|----------|
| 700 | 20 | 4 | 1 | 0 | 0 |
| 701 | 20 | 0 | 3 | 2 | 1 |[
{"name":"item-a","rest":0,"paid":0,"canceled":0,"refunded":0},
{"name":"item-b","rest":4,"paid":4,"canceled":2,"refunded":1},
{"name":"item-c","rest":0,"paid":0,"canceled":0,"refunded":0}
]Products with no matching invoice rows must still be returned with zero totals.
product:
| product_id | name |
|------------|--------|
| 501 | c-item |
| 101 | a-item |
| 301 | b-item |
invoice:
| invoice_id | product_id | rest | paid | canceled | refunded |
|------------|------------|------|------|----------|----------|
| 1 | 501 | 9 | 0 | 0 | 0 |
| 2 | 301 | 0 | 6 | 1 | 0 |
| 3 | 101 | 5 | 2 | 0 | 2 |[
{"name":"a-item","rest":5,"paid":2,"canceled":0,"refunded":2},
{"name":"b-item","rest":0,"paid":6,"canceled":1,"refunded":0},
{"name":"c-item","rest":9,"paid":0,"canceled":0,"refunded":0}
]Ordering is by product name, not by product_id or invoice sequence.
Constraints