{"publisher":"WESWOO","id":"month-end-by-location","engine":"SQLite","title":{"zh":"查看样例记录与复核查询","en":"Inspect sample records and query"},"setup_sql":"CREATE TABLE scope(site TEXT PRIMARY KEY); INSERT INTO scope VALUES ('North'),('South');\nCREATE TABLE snapshot(day TEXT,site TEXT,variant TEXT,available INT,committed INT,unavailable INT,incoming INT,cost INT,PRIMARY KEY(day,site,variant));\nINSERT INTO snapshot VALUES ('2026-10-31','North','T',5,2,1,3,500),('2026-10-31','South','T',2,0,0,4,500),\n ('2026-11-01','North','T',10,0,0,0,500),('2026-09-30','North','T',5,2,1,3,500),('2026-09-30','South','T',2,0,0,4,500);\nCREATE TABLE cutoff(day TEXT); INSERT INTO cutoff VALUES ('2026-10-31');","query_sql":"WITH balances AS (SELECT location.site,s.variant,s.cost,\n CASE WHEN cutoff.day<'2026-10-01' THEN available ELSE available+committed+unavailable END AS qty\n FROM scope location CROSS JOIN cutoff LEFT JOIN snapshot s ON location.site=s.site AND s.day=cutoff.day)\nSELECT site,SUM(qty),CASE WHEN COUNT(cost)=COUNT(*) AND COUNT(qty)=COUNT(*) THEN SUM(qty*cost) END AS value_cents\nFROM balances GROUP BY site ORDER BY site;","checks":[{"condition":"Positive stock at October close, incoming excluded","change_sql":"","expected":[["North",8,4000],["South",2,1000]],"observed":[["North",8,4000],["South",2,1000]]},{"condition":"Same physical quantities on September basis","change_sql":"UPDATE cutoff SET day='2026-09-30';","expected":[["North",5,2500],["South",2,1000]],"observed":[["North",5,2500],["South",2,1000]]},{"condition":"South snapshot missing","change_sql":"DELETE FROM snapshot WHERE day='2026-10-31' AND site='South';","expected":[["North",8,4000],["South",null,null]],"observed":[["North",8,4000],["South",null,null]]},{"condition":"South cost undocumented","change_sql":"UPDATE snapshot SET cost=NULL WHERE site='South';","expected":[["North",8,4000],["South",2,null]],"observed":[["North",8,4000],["South",2,null]]}]}