← SQL workplace practice

Stock layers and movement evidence · Original fictional data · Free files and full answers

Value each issue at its chronological moving weighted cost

Interleave receipts before issues on the same day, by id. A receipt adds quantity and acquisition value; an issue consumes up to available quantity at the current average. Return issue_id,moving_unit_cost,covered_units,moving_cogs_cents, money rounded2. Sort issue_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

  1. Interleave receipts before issues on the same day, by id. A receipt adds quantity and acquisition value; an issue consumes up to available quantity at the current average. Return issue_id,moving_unit_cost,covered_units,moving_cogs_cents, money rounded2. Sort issue_id.
  2. 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.
  3. 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, moving_unit_cost, covered_units, moving_cogs_cents. Sort order follows the task. Output has 5 rows; an empty CSV field means NULL.

Source data

lots

6 source rows · Download lots CSV

id INTEGERsku TEXTreceived INTEGERexpiry INTEGERunits INTEGERcost INTEGER
1A0205100
2A3104120
3A8306150
4B0128200
5B6405180
6C283500

issues

5 source rows · Download issues CSV

id INTEGERsku TEXTday INTEGERunits INTEGER
1A46
2A95
3B33
4B127
5C54

transfers

6 source rows · Download transfers CSV

id INTEGERpair_id TEXTwarehouse TEXTdelta INTEGER
1P1North-5
2P1South5
3P2North-3
4P2West2
5P3South-4
6P4West6
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.

Download complete SQLite setup

A method you can reuse

  1. Interleave receipts before issues on the same day, by id. A receipt adds quantity and acquisition value; an issue consumes up to available quantity at the current average. Return issue_id,moving_unit_cost,covered_units,moving_cogs_cents, money rounded2. Sort issue_id.
  2. For the first output record, issue_id = 1; moving_unit_cost = 108.89; covered_units = 6; moving_cogs_cents = 653.33.
  3. Inspect the source grain, keys and ties. Compare each result column with the full worked SQL and expected CSV.

Watch for this wrong turn

A final weighted purchase price leaks later receipts into earlier issues; FIFO is a different cost flow policy.

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 RECURSIVE e AS (SELECT sku,received day,0 kind,id,units,cost FROM lots UNION ALL SELECT sku,day,1,id,units,0 FROM issues), x AS (SELECT *,ROW_NUMBER() OVER(PARTITION BY sku ORDER BY day,kind,id) n FROM e), r(sku,n,qty,value,issue_id,unit_cost,covered,cogs) AS (SELECT DISTINCT sku,0,0,0.0,NULL,NULL,NULL,NULL FROM e UNION ALL SELECT r.sku,x.n,CASE WHEN kind=0 THEN r.qty+x.units ELSE MAX(0,r.qty-x.units) END,CASE WHEN kind=0 THEN r.value+x.units*x.cost WHEN r.qty>0 THEN r.value-MIN(r.qty,x.units)*r.value/r.qty ELSE r.value END,CASE WHEN kind=1 THEN x.id END,CASE WHEN kind=1 AND r.qty>0 THEN r.value/r.qty END,CASE WHEN kind=1 THEN MIN(r.qty,x.units) END,CASE WHEN kind=1 AND r.qty>0 THEN MIN(r.qty,x.units)*r.value/r.qty WHEN kind=1 THEN 0 END FROM r JOIN x ON x.sku=r.sku AND x.n=r.n+1) SELECT issue_id,ROUND(unit_cost,2) moving_unit_cost,covered covered_units,ROUND(cogs,2) moving_cogs_cents FROM r WHERE issue_id IS NOT NULL ORDER BY issue_id;
"issue_id","moving_unit_cost","covered_units","moving_cogs_cents"
"1","108.89","6","653.33"
"2","136.3","5","681.48"
"3","200","3","600"
"4","190","7","1330"
"5","500","3","1500"
Reference SQL · Complete expected CSV · Empty answer CSV

Choose another SQL task · Original data exception cases · Larger workplace projects