Stock layers and movement evidence · Original fictional data · Free files and full answers
Allocate issues to FIFO acquisition layers
All supplied issue sequences have no later receipt before an earlier shortage except SKU C with a terminal shortage. Allocate each issue across lots in received,id order, considering cumulative quantities. Return issue_id,lot_id,allocated_units for positive covered quantities only, sorted issue_id,lot_id.
Use this fixed business case to build a checked CSV and a replayable SQL handoff. No account required. Scenario datasets are shared across related exercises; each exercise has its own decision and output contract.
Your output contract
- All supplied issue sequences have no later receipt before an earlier shortage except SKU C with a terminal shortage. Allocate each issue across lots in received,id order, considering cumulative quantities. Return issue_id,lot_id,allocated_units for positive covered quantities only, sorted issue_id,lot_id.
- Return exactly the declared columns and order. Money uses integer cents; time and date units follow the task and field dictionary. NULL is unknown and differs from numeric zero.
- These are original fictional records and exercise policies. Scenario data may be shared with related tasks, whose decisions and output contracts differ.
Return columns exactly as issue_id, lot_id, allocated_units. Sort order follows the task. Output has 8 rows; an empty CSV field means NULL.
Source data
lots
6 source rows · Download lots CSV
| id INTEGER | sku TEXT | received INTEGER | expiry INTEGER | units INTEGER | cost INTEGER |
|---|---|---|---|---|---|
| 1 | A | 0 | 20 | 5 | 100 |
| 2 | A | 3 | 10 | 4 | 120 |
| 3 | A | 8 | 30 | 6 | 150 |
| 4 | B | 0 | 12 | 8 | 200 |
| 5 | B | 6 | 40 | 5 | 180 |
| 6 | C | 2 | 8 | 3 | 500 |
issues
5 source rows · Download issues CSV
| id INTEGER | sku TEXT | day INTEGER | units INTEGER |
|---|---|---|---|
| 1 | A | 4 | 6 |
| 2 | A | 9 | 5 |
| 3 | B | 3 | 3 |
| 4 | B | 12 | 7 |
| 5 | C | 5 | 4 |
transfers
6 source rows · Download transfers CSV
| id INTEGER | pair_id TEXT | warehouse TEXT | delta INTEGER |
|---|---|---|---|
| 1 | P1 | North | -5 |
| 2 | P1 | South | 5 |
| 3 | P2 | North | -3 |
| 4 | P2 | West | 2 |
| 5 | P3 | South | -4 |
| 6 | P4 | West | 6 |
Field dictionary and schema
lots: id INTEGER — Stable source record identifier; not a row position. sku TEXT — Literal business key or category label. Match exactly and retain NULL where present. received INTEGER — Lot receipt day index. expiry INTEGER — Lot expiration day; issue on or after this day is expired. units INTEGER — Physical unit quantity, not a monetary amount. cost INTEGER — Acquisition cost in integer cents per physical unit. issues: id INTEGER — Stable source record identifier; not a row position. sku TEXT — Literal business key or category label. Match exactly and retain NULL where present. day INTEGER — Integer day index, or ISO YYYY-MM-DD date when stored as text; the task declares cutoff semantics. units INTEGER — Physical unit quantity, not a monetary amount. transfers: id INTEGER — Stable source record identifier; not a row position. pair_id TEXT — Foreign business-record identifier; match to the named source key. warehouse TEXT — Literal business key or category label. Match exactly and retain NULL where present. delta INTEGER — Signed movement: positive adds, negative removes.
A method you can reuse
- All supplied issue sequences have no later receipt before an earlier shortage except SKU C with a terminal shortage. Allocate each issue across lots in received,id order, considering cumulative quantities. Return issue_id,lot_id,allocated_units for positive covered quantities only, sorted issue_id,lot_id.
- For the first output record, issue_id = 1; lot_id = 1; allocated_units = 5.
- Inspect the source grain, keys and ties. Compare each result column with the full worked SQL and expected CSV.
Watch for this wrong turn
Joining each issue to one earliest lot fails when an issue spans several cost layers.
Run and check in SQLite
Your SQL runs locally in the existing SQLite worker. The checker compares every column, row, type, value and row order with this fixed output. It accepts equivalent SELECT or WITH queries. Browser storage keeps your SQL on this device when available.
Complete worked SQL and expected output
WITH l AS (SELECT *,SUM(units) OVER(PARTITION BY sku ORDER BY received,id)-units lo,SUM(units) OVER(PARTITION BY sku ORDER BY received,id) hi FROM lots), i AS (SELECT *,SUM(units) OVER(PARTITION BY sku ORDER BY day,id)-units lo,SUM(units) OVER(PARTITION BY sku ORDER BY day,id) hi FROM issues) SELECT i.id issue_id,l.id lot_id,MIN(i.hi,l.hi)-MAX(i.lo,l.lo) allocated_units FROM i JOIN l ON i.sku=l.sku AND l.received<=i.day AND i.lo<l.hi AND l.lo<i.hi ORDER BY i.id,l.id;
"issue_id","lot_id","allocated_units" "1","1","5" "1","2","1" "2","2","3" "2","3","2" "3","4","3" "4","4","5" "4","5","2" "5","6","3"Reference SQL · Complete expected CSV · Empty answer CSV
Choose another SQL task · Original data exception cases · Larger workplace projects