Queries · q190 · Director of Fulfilment Operations · week 1, night 3
“Unlike cycle_count (always exactly 1 unit), kind='adjustment' movements vary widely (avg ~10 units, max 192) — what is the monthly scale, direction (shrinkage vs gain) and estimated value of this stream, and how does it trend across 2025?”
Fictional people at a fictional company, each played by a language-model agent. Who they are and what they are told
Week 1; shared with Tobias Lindqvist, Chief Financial Officer
Auditors and merchants question inventory accuracy. How often do counts, adjustments and reservations disagree with recorded stock, where, and why?
The manager, at the start of week 1
Subject: Inventory accuracy — what I need for Friday's memo
Fatima,
Tobias and I need to give the auditors (and our bigger merchants) a credible answer on how trustworthy our stock numbers are, where it breaks down, and whether it's getting better or worse. This feeds the joint audit response and will likely shape where we invest in warehouse process fixes next quarter — so I'd rather have three sharp answers than ten partial ones. Please use your nightly slots on these, in rough priority order:
If you can only get to three this week, do 1, 2 and 3 — those are the ones Tobias and I will lean on most heavily in the audit conversation. Happy to talk through trade-offs before you lock in Monday night's requests.
Thanks, Tomasz
Subject: Finance questions for the inventory accuracy review
Hi Fatima,
A few things from the finance side as you dig into this with Tomasz:
Keep it high level for now — happy to go deeper once you have an initial read.
Thanks, Tobias
The analyst · week 1, night 3, request 1 of 3
Night 2's magnitude check (report 1) landed a bigger finding than expected: cycle_count movements are literally always exactly ±1 unit (min=max=1, 100% of 10,193 events) — confirmed, not a query artifact, since adjustment/receipt/return/shipment all show wide, varied distributions in the same query. That means my night-1 "scale of the problem" was built entirely on a stream that, by construction, can never show a large miss. Meanwhile kind='adjustment' events are rare (407 in the year) but average ~10 units and run up to 192 units per event — a completely different animal, and likely the stream that actually matters for Tobias's write-off/valuation question. I under-covered this on night 1 and need to correct course before locking the memo.
Also from night 2: report 2 confirms cycle-count activity is 100% in 'pick' locations (120 of 157 pick locations active, 0 of 157 bulk, 0 of 157 returns locations) — a clean, decisive answer to "which location kind" for that stream, so I don't need to spend more on that question. And report 3 shows reservations are overwhelmingly reliable (99.3% released_on_time, only 0.3% ever stuck past expiry) — Q4 looks like a non-issue, I'll give it one more sanity pass later if time allows but it's not using a slot tonight.
Tonight I'm redirecting all three slots to properly characterize the 'adjustment' stream, since it's likely the financially material one: (1) its own monthly scale/trend, parallel to night 1's cycle_count view, so I can compare the two streams side by side in the memo; (2) the actual top individual events (only 407 total, so a top-30 detail table efficiently reveals whether size concentrates by warehouse/tenant/category/product all at once, rather than slicing a thin 407-row base three more ways); (3) a definitional cross-check against the separate stock_adjustments table, since that table is described as the human-entered correction record with a reason, and I want to confirm it's the same universe as stock_movements kind='adjustment' before I tell auditors which number is authoritative.
The engineer · 2 dry runs on the empty schema, then the submission
WITH variant_cost AS ( SELECT sp.variant_id, sp.unit_cost, sp.currency_code, ROW_NUMBER() OVER (PARTITION BY sp.variant_id ORDER BY sp.is_preferred DESC, sp.unit_cost ASC) AS rn FROM supplier_products sp WHERE sp.unit_cost IS NOT NULL ), best_cost AS ( SELECT variant_id, unit_cost, currency_code FROM variant_cost WHERE rn = 1 ), movements AS ( SELECT sm.id, sm.variant_id, sm.quantity, sm.occurred_at, CAST(sm.occurred_at AS date) AS occ_date FROM stock_movements sm WHERE sm.kind = 'adjustment' AND sm.occurred_at >= DATE '2025-01-01' AND sm.occurred_at < DATE '2026-01-01' ), valued AS ( SELECT m.id, m.quantity, date_trunc('month', m.occurred_at) AS month_start, CASE WHEN bc.currency_code = 'EUR' THEN bc.unit_cost WHEN bc.currency_code IS NULL THEN NULL ELSE bc.unit_cost / er.rate END AS unit_cost_eur FROM movements m LEFT JOIN best_cost bc ON bc.variant_id = m.variant_id LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = bc.currency_code AND er.as_of = m.occ_date ) SELECT to_char(month_start, 'YYYY-MM') AS month, COUNT(*) AS adjustment_events, SUM(CASE WHEN quantity < 0 THEN -quantity ELSE 0 END) AS units_short, SUM(CASE WHEN quantity > 0 THEN quantity ELSE 0 END) AS units_over, SUM(quantity) AS net_units_variance, SUM(ABS(quantity)) AS abs_units_variance, ROUND(SUM(ABS(quantity) * COALESCE(unit_cost_eur,0))::numeric, 2) AS estimated_value_eur, ROUND(AVG(ABS(quantity))::numeric, 1) AS avg_abs_units_per_event FROM valued GROUP BY month_start ORDER BY month_start
WITH variant_cost AS ( SELECT sp.variant_id, sp.unit_cost, sp.currency_code, ROW_NUMBER() OVER (PARTITION BY sp.variant_id ORDER BY sp.is_preferred DESC, sp.unit_cost ASC) AS rn FROM supplier_products sp WHERE sp.unit_cost IS NOT NULL),best_cost AS ( SELECT variant_id, unit_cost, currency_code FROM variant_cost WHERE rn = 1),movements AS ( SELECT sm.id, sm.variant_id, sm.quantity, sm.occurred_at, CAST(sm.occurred_at AS date) AS occ_date FROM stock_movements sm WHERE sm.kind = 'adjustment' AND sm.occurred_at >= DATE '2025-01-01' AND sm.occurred_at < DATE '2026-01-01'),valued AS ( SELECT m.id, m.quantity, date_trunc('month', m.occurred_at) AS month_start, CASE WHEN bc.currency_code = 'EUR' THEN bc.unit_cost WHEN bc.currency_code IS NULL THEN NULL ELSE bc.unit_cost / er.rate END AS unit_cost_eur FROM movements m LEFT JOIN best_cost bc ON bc.variant_id = m.variant_id LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = bc.currency_code AND er.as_of = m.occ_date)SELECT to_char(month_start, 'YYYY-MM') AS month, strftime(month_start, '%Y-%m') AS month, COUNT(*) AS adjustment_events, SUM(CASE WHEN quantity < 0 THEN -quantity ELSE 0 END) AS units_short, SUM(CASE WHEN quantity > 0 THEN quantity ELSE 0 END) AS units_over, SUM(quantity) AS net_units_variance, SUM(ABS(quantity)) AS abs_units_variance, ROUND(SUM(ABS(quantity) * COALESCE(unit_cost_eur,0))::numeric, 2) AS estimated_value_eur, ROUND(AVG(ABS(quantity))::numeric, 1) AS avg_abs_units_per_eventFROM valuedGROUP BY month_startORDER BY month_start
“Query compiles. Submitting.”
“Submitted. Summary of approach:
- Grain: one row per calendar month (2025-01 … 2025-12), restricted to stock_movements with kind='adjustment' and occurred_at in a half-open 2025 window.
- Direction/scale: units_short (sum of negative quantities, made positive) and units_over (sum of positive quantities) separate shrinkage from gains; net_units_variance keeps the sign (negative = net shrinkage), abs_units_variance is the total movement scale regardless of direction.
- Costing/FX: for each variant, picked the unit cost from supplier_products preferring is_preferred=true, falling back to the cheapest unit_cost otherwise (via ROW_NUMBER() OVER (... ORDER BY is_preferred DESC, unit_cost ASC)), matching the method used in the earlier stock-count-discrepancy report. Converted that cost to EUR using exchange_rates (base_code='EUR', quote_code = supplier's currency) as of the movement's own date.
- estimated_value_eur = sum of abs(quantity) * unit_cost_eur; variants lacking any supplier cost contribute 0 to this value (flagged in the submit note) but are still counted in the unit/event columns.
- Ordered ascending by month for trend reading.”
As Fatima Zahra saw it, from the small dataset the agents worked on
month | adjustment_events | units_short | units_over | net_units_variance | abs_units_variance | estimated_value_eur | avg_abs_units_per_event --------+-------------------+-------------+------------+--------------------+--------------------+---------------------+------------------------ 2025-05 | 1 | 1 | 0 | -1 | 1 | 117.50 | 1.0 2025-12 | 406 | 0 | 4066 | 4066 | 4066 | 138023.65 | 10.0 (2 rows)
The memo and the queries that followed; links found by title, so inferred
Later queries that build on this report:
It builds on:
Changed only where the benchmark needs the same result on every system
WITH variant_cost AS ( SELECT sp.variant_id, sp.unit_cost, sp.currency_code, ROW_NUMBER() OVER (PARTITION BY sp.variant_id ORDER BY sp.is_preferred DESC, sp.unit_cost ASC) AS rn SELECT sp.variant_id AS variant_id, sp.unit_cost AS unit_cost, sp.currency_code AS currency_code, ROW_NUMBER() OVER (PARTITION BY sp.variant_id ORDER BY sp.is_preferred DESC NULLS FIRST, sp.unit_cost ASC NULLS LAST, sp.supplier_id NULLS LAST) AS rn FROM supplier_products sp WHERE sp.unit_cost IS NOT NULL),best_cost AS ( SELECT variant_id, unit_cost, currency_code FROM variant_cost WHERE rn = 1),movements AS ( SELECT sm.id, sm.variant_id, sm.quantity, sm.occurred_at, SELECT sm.id AS id, sm.variant_id AS variant_id, sm.quantity AS quantity, sm.occurred_at AS occurred_at, CAST(sm.occurred_at AS date) AS occ_date FROM stock_movements sm WHERE sm.kind = 'adjustment' AND sm.occurred_at >= DATE '2025-01-01' AND sm.occurred_at < DATE '2026-01-01'),valued AS ( SELECT m.id, m.quantity, SELECT m.id AS id, m.quantity AS quantity, date_trunc('month', m.occurred_at) AS month_start, CASE WHEN bc.currency_code = 'EUR' THEN bc.unit_cost WHEN bc.currency_code IS NULL THEN NULL ELSE bc.unit_cost / er.rate ELSE CAST(bc.unit_cost / er.rate AS NUMERIC(18,6)) END AS unit_cost_eur FROM movements m LEFT JOIN best_cost bc ON bc.variant_id = m.variant_id LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = bc.currency_code AND er.as_of = m.occ_date)SELECT strftime(month_start, '%Y-%m') AS month, {{year_month(month_start)}} AS month, COUNT(*) AS adjustment_events, SUM(CASE WHEN quantity < 0 THEN -quantity ELSE 0 END) AS units_short, SUM(CASE WHEN quantity > 0 THEN quantity ELSE 0 END) AS units_over, SUM(quantity) AS net_units_variance, SUM(ABS(quantity)) AS abs_units_variance, ROUND(SUM(ABS(quantity) * COALESCE(unit_cost_eur,0))::numeric, 2) AS estimated_value_eur, ROUND(AVG(ABS(quantity))::numeric, 1) AS avg_abs_units_per_event SUM(ABS(quantity) * COALESCE(unit_cost_eur,0)) AS estimated_value_eur, AVG(CAST(ABS(quantity) AS NUMERIC(18,6))) AS avg_abs_units_per_eventFROM valuedGROUP BY month_startORDER BY month_startORDER BY month_start NULLS LAST
WITH variant_cost AS ( SELECT sp.variant_id AS variant_id, sp.unit_cost AS unit_cost, sp.currency_code AS currency_code, ROW_NUMBER() OVER (PARTITION BY sp.variant_id ORDER BY sp.is_preferred DESC NULLS FIRST, sp.unit_cost ASC NULLS LAST, sp.supplier_id NULLS LAST) AS rn FROM supplier_products sp WHERE sp.unit_cost IS NOT NULL ), best_cost AS ( SELECT variant_id, unit_cost, currency_code FROM variant_cost WHERE rn = 1 ), movements AS ( SELECT sm.id AS id, sm.variant_id AS variant_id, sm.quantity AS quantity, sm.occurred_at AS occurred_at, CAST(sm.occurred_at AS date) AS occ_date FROM stock_movements sm WHERE sm.kind = 'adjustment' AND sm.occurred_at >= DATE '2025-01-01' AND sm.occurred_at < DATE '2026-01-01' ), valued AS ( SELECT m.id AS id, m.quantity AS quantity, date_trunc('month', m.occurred_at) AS month_start, CASE WHEN bc.currency_code = 'EUR' THEN bc.unit_cost WHEN bc.currency_code IS NULL THEN NULL ELSE CAST(bc.unit_cost / er.rate AS NUMERIC(18,6)) END AS unit_cost_eur FROM movements m LEFT JOIN best_cost bc ON bc.variant_id = m.variant_id LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = bc.currency_code AND er.as_of = m.occ_date ) SELECT strftime(month_start, '%Y-%m') AS month, COUNT(*) AS adjustment_events, SUM(CASE WHEN quantity < 0 THEN -quantity ELSE 0 END) AS units_short, SUM(CASE WHEN quantity > 0 THEN quantity ELSE 0 END) AS units_over, SUM(quantity) AS net_units_variance, SUM(ABS(quantity)) AS abs_units_variance, SUM(ABS(quantity) * COALESCE(unit_cost_eur,0)) AS estimated_value_eur, AVG(CAST(ABS(quantity) AS NUMERIC(18,6))) AS avg_abs_units_per_event FROM valued GROUP BY month_start ORDER BY month_start NULLS LAST
WITH variant_cost AS ( SELECT sp.variant_id AS variant_id, sp.unit_cost AS unit_cost, sp.currency_code AS currency_code, ROW_NUMBER() OVER (PARTITION BY sp.variant_id ORDER BY sp.is_preferred DESC NULLS FIRST, sp.unit_cost ASC NULLS LAST, sp.supplier_id NULLS LAST) AS rn FROM supplier_products sp WHERE sp.unit_cost IS NOT NULL ), best_cost AS ( SELECT variant_id, unit_cost, currency_code FROM variant_cost WHERE rn = 1 ), movements AS ( SELECT sm.id AS id, sm.variant_id AS variant_id, sm.quantity AS quantity, sm.occurred_at AS occurred_at, CAST(sm.occurred_at AS date) AS occ_date FROM stock_movements sm WHERE sm.kind = 'adjustment' AND sm.occurred_at >= DATE '2025-01-01' AND sm.occurred_at < DATE '2026-01-01' ), valued AS ( SELECT m.id AS id, m.quantity AS quantity, date_trunc('month', m.occurred_at) AS month_start, CASE WHEN bc.currency_code = 'EUR' THEN bc.unit_cost WHEN bc.currency_code IS NULL THEN NULL ELSE CAST(bc.unit_cost / er.rate AS NUMERIC(18,6)) END AS unit_cost_eur FROM movements m LEFT JOIN best_cost bc ON bc.variant_id = m.variant_id LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = bc.currency_code AND er.as_of = m.occ_date ) SELECT to_char(month_start, 'YYYY-MM') AS month, COUNT(*) AS adjustment_events, SUM(CASE WHEN quantity < 0 THEN -quantity ELSE 0 END) AS units_short, SUM(CASE WHEN quantity > 0 THEN quantity ELSE 0 END) AS units_over, SUM(quantity) AS net_units_variance, SUM(ABS(quantity)) AS abs_units_variance, SUM(ABS(quantity) * COALESCE(unit_cost_eur,0)) AS estimated_value_eur, AVG(CAST(ABS(quantity) AS NUMERIC(18,6))) AS avg_abs_units_per_event FROM valued GROUP BY month_start ORDER BY month_start NULLS LAST
WITH variant_cost AS ( SELECT sp.variant_id AS variant_id, sp.unit_cost AS unit_cost, sp.currency_code AS currency_code, ROW_NUMBER() OVER (PARTITION BY sp.variant_id ORDER BY sp.is_preferred DESC NULLS FIRST, sp.unit_cost ASC NULLS LAST, sp.supplier_id NULLS LAST) AS rn FROM supplier_products sp WHERE sp.unit_cost IS NOT NULL ), best_cost AS ( SELECT variant_id, unit_cost, currency_code FROM variant_cost WHERE rn = 1 ), movements AS ( SELECT sm.id AS id, sm.variant_id AS variant_id, sm.quantity AS quantity, sm.occurred_at AS occurred_at, CAST(sm.occurred_at AS date) AS occ_date FROM stock_movements sm WHERE sm.kind = 'adjustment' AND sm.occurred_at >= DATE '2025-01-01' AND sm.occurred_at < DATE '2026-01-01' ), valued AS ( SELECT m.id AS id, m.quantity AS quantity, date_trunc('month', m.occurred_at) AS month_start, CASE WHEN bc.currency_code = 'EUR' THEN bc.unit_cost WHEN bc.currency_code IS NULL THEN NULL ELSE CAST(bc.unit_cost / er.rate AS NUMERIC(18,6)) END AS unit_cost_eur FROM movements m LEFT JOIN best_cost bc ON bc.variant_id = m.variant_id LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = bc.currency_code AND er.as_of = m.occ_date ) SELECT to_char(CAST(month_start AS timestamp), 'YYYY-MM') AS month, COUNT(*) AS adjustment_events, SUM(CASE WHEN quantity < 0 THEN -quantity ELSE 0 END) AS units_short, SUM(CASE WHEN quantity > 0 THEN quantity ELSE 0 END) AS units_over, SUM(quantity) AS net_units_variance, SUM(ABS(quantity)) AS abs_units_variance, SUM(ABS(quantity) * COALESCE(unit_cost_eur,0)) AS estimated_value_eur, AVG(CAST(ABS(quantity) AS NUMERIC(18,6))) AS avg_abs_units_per_event FROM valued GROUP BY month_start ORDER BY month_start NULLS LAST
WITH variant_cost AS ( SELECT sp.variant_id AS variant_id, sp.unit_cost AS unit_cost, sp.currency_code AS currency_code, ROW_NUMBER() OVER (PARTITION BY sp.variant_id ORDER BY sp.is_preferred DESC NULLS FIRST, sp.unit_cost ASC NULLS LAST, sp.supplier_id NULLS LAST) AS rn FROM supplier_products sp WHERE sp.unit_cost IS NOT NULL ), best_cost AS ( SELECT variant_id, unit_cost, currency_code FROM variant_cost WHERE rn = 1 ), movements AS ( SELECT sm.id AS id, sm.variant_id AS variant_id, sm.quantity AS quantity, sm.occurred_at AS occurred_at, CAST(sm.occurred_at AS date) AS occ_date FROM stock_movements sm WHERE sm.kind = 'adjustment' AND sm.occurred_at >= DATE '2025-01-01' AND sm.occurred_at < DATE '2026-01-01' ), valued AS ( SELECT m.id AS id, m.quantity AS quantity, date_trunc('month', m.occurred_at) AS month_start, CASE WHEN bc.currency_code = 'EUR' THEN bc.unit_cost WHEN bc.currency_code IS NULL THEN NULL ELSE CAST(bc.unit_cost / er.rate AS NUMERIC(18,6)) END AS unit_cost_eur FROM movements m LEFT JOIN best_cost bc ON bc.variant_id = m.variant_id LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = bc.currency_code AND er.as_of = m.occ_date ) SELECT formatDateTime(month_start, '%Y-%m') AS month, COUNT(*) AS adjustment_events, SUM(CASE WHEN quantity < 0 THEN -quantity ELSE 0 END) AS units_short, SUM(CASE WHEN quantity > 0 THEN quantity ELSE 0 END) AS units_over, SUM(quantity) AS net_units_variance, SUM(ABS(quantity)) AS abs_units_variance, SUM(ABS(quantity) * COALESCE(unit_cost_eur,0)) AS estimated_value_eur, AVG(CAST(ABS(quantity) AS NUMERIC(18,6))) AS avg_abs_units_per_event FROM valued GROUP BY month_start ORDER BY month_start NULLS LAST
The agents' own records, as the role play wrote them; only the personal data of the person who ran it is redacted
Report request from Fatima Zahra (analyst for the Director of Fulfilment Operations): Title: Scale of adjustment-kind stock movements by month, 2025 Question: Unlike cycle_count (always exactly 1 unit), kind='adjustment' movements vary widely (avg ~10 units, max 192) — what is the monthly scale, direction (shrinkage vs gain) and estimated value of this stream, and how does it trend across 2025? Grain: calendar month (Jan 2025 - Dec 2025) Columns: - month (YYYY-MM) - adjustment_events (count of stock_movements rows with kind='adjustment') - units_short (sum of abs(quantity) where quantity<0) - units_over (sum of quantity where quantity>0) - net_units_variance (signed sum; negative = net shrinkage) - abs_units_variance (sum of abs(quantity)) - estimated_value_eur (sum of abs(quantity) * unit cost, converted to EUR — same costing/FX method as prior reports: preferred-then-cheapest supplier_products.unit_cost, converted via exchange_rates on the movement's occurred_at date) - avg_abs_units_per_event (rounded 1dp) Details: Source: stock_movements where kind='adjustment', occurred_at in 2025-01-01 to 2025-12-31 (use a half-open bound occurred_at >= 2025-01-01 and < 2026-01-01 as done previously). Same costing/FX approach as the 'Scale of stock count discrepancies by month' report from night 1. Order by month ascending. Check your query with the warehouse's dry_run tool, then hand it in with submit.
dry_runWITH variant_cost AS ( SELECT sp.variant_id, sp.unit_cost, sp.currency_code, ROW_NUMBER() OVER (PARTITION BY sp.variant_id ORDER BY sp.is_preferred DESC, sp.unit_cost ASC) AS rn FROM supplier_products sp WHERE sp.unit_cost IS NOT NULL ), best_cost AS ( SELECT variant_id, unit_cost, currency_code FROM variant_cost WHERE rn = 1 ), movements AS ( SELECT sm.id, sm.variant_id, sm.quantity, sm.occurred_at, CAST(sm.occurred_at AS date) AS occ_date FROM stock_movements sm WHERE sm.kind = 'adjustment' AND sm.occurred_at >= DATE '2025-01-01' AND sm.occurred_at < DATE '2026-01-01' ), valued AS ( SELECT m.id, m.quantity, date_trunc('month', m.occurred_at) AS month_start, CASE WHEN bc.currency_code = 'EUR' THEN bc.unit_cost WHEN bc.currency_code IS NULL THEN NULL ELSE bc.unit_cost / er.rate END AS unit_cost_eur FROM movements m LEFT JOIN best_cost bc ON bc.variant_id = m.variant_id LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = bc.currency_code AND er.as_of = m.occ_date ) SELECT to_char(month_start, 'YYYY-MM') AS month, COUNT(*) AS adjustment_events, SUM(CASE WHEN quantity < 0 THEN -quantity ELSE 0 END) AS units_short, SUM(CASE WHEN quantity > 0 THEN quantity ELSE 0 END) AS units_over, SUM(quantity) AS net_units_variance, SUM(ABS(quantity)) AS abs_units_variance, ROUND(SUM(ABS(quantity) * COALESCE(unit_cost_eur,0))::numeric, 2) AS estimated_value_eur, ROUND(AVG(ABS(quantity))::numeric, 1) AS avg_abs_units_per_event FROM valued GROUP BY month_start ORDER BY month_start
STATUS: REJECTED - does not compile: Catalog Error: Scalar Function with name to_char does not exist!
dry_runWITH variant_cost AS ( SELECT sp.variant_id, sp.unit_cost, sp.currency_code, ROW_NUMBER() OVER (PARTITION BY sp.variant_id ORDER BY sp.is_preferred DESC, sp.unit_cost ASC) AS rn FROM supplier_products sp WHERE sp.unit_cost IS NOT NULL ), best_cost AS ( SELECT variant_id, unit_cost, currency_code FROM variant_cost WHERE rn = 1 ), movements AS ( SELECT sm.id, sm.variant_id, sm.quantity, sm.occurred_at, CAST(sm.occurred_at AS date) AS occ_date FROM stock_movements sm WHERE sm.kind = 'adjustment' AND sm.occurred_at >= DATE '2025-01-01' AND sm.occurred_at < DATE '2026-01-01' ), valued AS ( SELECT m.id, m.quantity, date_trunc('month', m.occurred_at) AS month_start, CASE WHEN bc.currency_code = 'EUR' THEN bc.unit_cost WHEN bc.currency_code IS NULL THEN NULL ELSE bc.unit_cost / er.rate END AS unit_cost_eur FROM movements m LEFT JOIN best_cost bc ON bc.variant_id = m.variant_id LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = bc.currency_code AND er.as_of = m.occ_date ) SELECT strftime(month_start, '%Y-%m') AS month, COUNT(*) AS adjustment_events, SUM(CASE WHEN quantity < 0 THEN -quantity ELSE 0 END) AS units_short, SUM(CASE WHEN quantity > 0 THEN quantity ELSE 0 END) AS units_over, SUM(quantity) AS net_units_variance, SUM(ABS(quantity)) AS abs_units_variance, ROUND(SUM(ABS(quantity) * COALESCE(unit_cost_eur,0))::numeric, 2) AS estimated_value_eur, ROUND(AVG(ABS(quantity))::numeric, 1) AS avg_abs_units_per_event FROM valued GROUP BY month_start ORDER BY month_start
STATUS: COMPILES (dry run on the empty schema, no data read) COLUMNS: month, adjustment_events, units_short, units_over, net_units_variance, abs_units_variance, estimated_value_eur, avg_abs_units_per_event
submitWITH variant_cost AS ( SELECT sp.variant_id, sp.unit_cost, sp.currency_code, ROW_NUMBER() OVER (PARTITION BY sp.variant_id ORDER BY sp.is_preferred DESC, sp.unit_cost ASC) AS rn FROM supplier_products sp WHERE sp.unit_cost IS NOT NULL ), best_cost AS ( SELECT variant_id, unit_cost, currency_code FROM variant_cost WHERE rn = 1 ), movements AS ( SELECT sm.id, sm.variant_id, sm.quantity, sm.occurred_at, CAST(sm.occurred_at AS date) AS occ_date FROM stock_movements sm WHERE sm.kind = 'adjustment' AND sm.occurred_at >= DATE '2025-01-01' AND sm.occurred_at < DATE '2026-01-01' ), valued AS ( SELECT m.id, m.quantity, date_trunc('month', m.occurred_at) AS month_start, CASE WHEN bc.currency_code = 'EUR' THEN bc.unit_cost WHEN bc.currency_code IS NULL THEN NULL ELSE bc.unit_cost / er.rate END AS unit_cost_eur FROM movements m LEFT JOIN best_cost bc ON bc.variant_id = m.variant_id LEFT JOIN exchange_rates er ON er.base_code = 'EUR' AND er.quote_code = bc.currency_code AND er.as_of = m.occ_date ) SELECT strftime(month_start, '%Y-%m') AS month, COUNT(*) AS adjustment_events, SUM(CASE WHEN quantity < 0 THEN -quantity ELSE 0 END) AS units_short, SUM(CASE WHEN quantity > 0 THEN quantity ELSE 0 END) AS units_over, SUM(quantity) AS net_units_variance, SUM(ABS(quantity)) AS abs_units_variance, ROUND(SUM(ABS(quantity) * COALESCE(unit_cost_eur,0))::numeric, 2) AS estimated_value_eur, ROUND(AVG(ABS(quantity))::numeric, 1) AS avg_abs_units_per_event FROM valued GROUP BY month_start ORDER BY month_start
Submitted to tonight's batch. Your work on this request is done.