{"id":"business-hours-fulfillment","publisher":"WESWOO","engine":"SQLite","title":{"zh":"查看样例与复核查询","en":"View records and verification query"},"setup_sql":"-- Minute offsets from Friday midnight on the shop's local work calendar; target 120 open minutes.\nCREATE TABLE hours(start INT,end INT);\nINSERT INTO hours VALUES (540,1020),(4860,5340);\nCREATE TABLE orders(id TEXT PRIMARY KEY,placed INT,fully_shipped INT,first_shipped INT);\nINSERT INTO orders VALUES ('A',960,4920,995),('B',990,4980,1000),('C',990,NULL,NULL),('D',4990,NULL,NULL);\nCREATE TABLE cutoff(asof INT); INSERT INTO cutoff VALUES (5000);","query_sql":"WITH clocks AS (SELECT o.id,o.fully_shipped,c.asof,\n SUM(MAX(0,MIN(c.asof,h.end)-MAX(o.placed,h.start))) AS age,\n SUM(MAX(0,MIN(COALESCE(o.fully_shipped,c.asof),h.end)-MAX(o.placed,h.start))) AS used\n FROM orders o CROSS JOIN hours h CROSS JOIN cutoff c GROUP BY o.id)\nSELECT id,CASE WHEN age<120 THEN 'not mature' WHEN fully_shipped IS NULL OR fully_shipped>asof THEN 'late open'\n WHEN used<=120 THEN 'on time' ELSE 'late complete' END AS finding FROM clocks ORDER BY id;","checks":[{"condition":"Friday to Monday, snapshot Monday 11:20","change_sql":"","expected":[["A","on time"],["B","late complete"],["C","late open"],["D","not mature"]],"observed":[["A","on time"],["B","late complete"],["C","late open"],["D","not mature"]]},{"condition":"Only B first parcel leaves earlier","change_sql":"UPDATE orders SET first_shipped=991 WHERE id='B';","expected":[["A","on time"],["B","late complete"],["C","late open"],["D","not mature"]],"observed":[["A","on time"],["B","late complete"],["C","late open"],["D","not mature"]]},{"condition":"C completes at minute 120","change_sql":"UPDATE orders SET fully_shipped=4950 WHERE id='C';","expected":[["A","on time"],["B","late complete"],["C","on time"],["D","not mature"]],"observed":[["A","on time"],["B","late complete"],["C","on time"],["D","not mature"]]},{"condition":"Monday closed at this location","change_sql":"DELETE FROM hours WHERE start=4860;","expected":[["A","not mature"],["B","not mature"],["C","not mature"],["D","not mature"]],"observed":[["A","not mature"],["B","not mature"],["C","not mature"],["D","not mature"]]}]}