{"publisher":"WESWOO","id":"supplier-mix-unit-cost","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 receipt(supplier TEXT,units INT,unit_cost INT);\nINSERT INTO receipt VALUES ('A',10,800),('B',30,1200);","query_sql":"SELECT SUM(units) AS units, CASE WHEN COUNT(unit_cost)=COUNT(*) THEN SUM(units*unit_cost) END AS total_cost,\n CASE WHEN COUNT(unit_cost)=COUNT(*) THEN ROUND(1.0*SUM(units*unit_cost)/NULLIF(SUM(units),0),2) END AS cents_per_unit FROM receipt;","checks":[{"condition":"Unequal receipt quantities","change_sql":"","expected":[[40,44000,1100]],"observed":[[40,44000,1100]]},{"condition":"Equal quantities","change_sql":"UPDATE receipt SET units=10 WHERE supplier='B';","expected":[[20,20000,1000]],"observed":[[20,20000,1000]]},{"condition":"One price unverified","change_sql":"UPDATE receipt SET unit_cost=NULL WHERE supplier='B';","expected":[[40,null,null]],"observed":[[40,null,null]]}]}