{"publisher":"WESWOO","id":"received-versus-invoiced","title":{"zh":"查看样例记录与复算 SQL","en":"Inspect the example records and SQL"},"scope":{"zh":"独立构造的运营记录。每个对照均从同一份初始数据重新开始。金额如无特别说明均为最小货币单位。","en":"Constructed operating records. Each check starts from the original setup. Monetary fields use minor currency units unless stated otherwise."},"setup_sql":"CREATE TABLE received(receipt_id TEXT PRIMARY KEY,po_line TEXT,accepted INT,rejected INT,unit_cost INT);\nCREATE TABLE invoiced(po_line TEXT,units INT);\nINSERT INTO received VALUES ('R1','PO1-A',6,1,900),('R2','PO1-A',2,0,900);\nINSERT INTO invoiced VALUES ('PO1-A',10);","query_sql":"WITH totals AS (SELECT po_line,SUM(accepted) a,SUM(rejected) r,SUM(accepted*unit_cost) v FROM received GROUP BY po_line)\nSELECT i.po_line,i.units,t.a,t.r,i.units-t.a AS unmatched_units,t.v AS accepted_value FROM invoiced i LEFT JOIN totals t USING(po_line);","checks":[{"condition":"Two receipts and a rejected unit","change_sql":"","expected":[["PO1-A",10,8,1,2,7200]],"observed":[["PO1-A",10,8,1,2,7200]]},{"condition":"Remaining units accepted later","change_sql":"INSERT INTO received VALUES ('R3','PO1-A',2,0,900);","expected":[["PO1-A",10,10,1,0,9000]],"observed":[["PO1-A",10,10,1,0,9000]]},{"condition":"Same SKU recorded against another purchase line","change_sql":"INSERT INTO received VALUES ('R3','PO2-A',2,0,900);","expected":[["PO1-A",10,8,1,2,7200]],"observed":[["PO1-A",10,8,1,2,7200]]}]}