Last updated
Find which order was packaged from which batch and in what quantity using recursive CTEs and row-wise allocation logic.
Find which order was packaged from which batch and in what quantity using recursive CTEs and row-wise allocation logic.
Your dataset contains two packaging allocation tables: 1. A Batches table with batch IDs and available quantity in each batch 2. An Orders table with order numbers and required quantity for each order The packaging company uses batches sequentially. It starts from the first batch, and only after that batch is exhausted, it moves to the next batch.
| Column | Type | Key / Notes |
|---|---|---|
| batch_id | VARCHAR(10) | |
| quantity | INT |
| Column | Type | Key / Notes |
|---|---|---|
| order_number | VARCHAR(10) | |
| quantity | INT |
Batches — 10 rows
| batch_id | quantity |
|---|---|
| BA | 10 |
| BB | 12 |
| BC | 10 |
| BD | 16 |
| BE | 15 |
| BF | 13 |
| BG | 19 |
| BH | 10 |
| BI | 14 |
| BJ | 16 |
Orders — 20 rows
| order_number | quantity |
|---|---|
| OA | 5 |
| OB | 4 |
| OC | 2 |
| OD | 8 |
| OE | 7 |
| OF | 10 |
| OG | 12 |
| OH | 6 |
| OI | 12 |
| OJ | 10 |
| OK | 8 |
| OL | 16 |
| OM | 8 |
| ON | 5 |
| OO | 3 |
| OP | 4 |
| OQ | 12 |
| OR | 9 |
| OS | 6 |
| OT | 10 |
Batch-wise Order Packaging Allocation: Which order was packaged from which batch and in what quantity? The final result table should have order_number, batch_id, and quantity columns.
Your output should look like this (partial example):
| order_number | batch_id | quantity |
|---|---|---|
| OA | BA | 5 |
| OB | BA | 4 |
| OC | BA | 1 |
| OC | BB | 1 |