{"publisher":"WESWOO","id":"first-basket-followup","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 purchase(id TEXT PRIMARY KEY,buyer TEXT,placed TEXT);\nCREATE TABLE line_item(order_id TEXT,sku TEXT);\nINSERT INTO purchase VALUES ('1','A','2026-01-01'),('2','A','2026-02-02'),('3','B','2026-01-05');\nINSERT INTO line_item VALUES ('1','Boot'),('1','Sock'),('2','Lace'),('3','Boot');","query_sql":"WITH ranked AS (\n SELECT *,ROW_NUMBER() OVER(PARTITION BY buyer ORDER BY placed,id) AS nth,\n COUNT(*) OVER(PARTITION BY buyer) AS purchases FROM purchase\n), cohort AS (\n SELECT DISTINCT l.sku,r.buyer,r.purchases FROM ranked r JOIN line_item l ON l.order_id=r.id WHERE r.nth=1\n) SELECT sku,COUNT(*) AS first_buyers,SUM(purchases>1) AS returned FROM cohort GROUP BY sku ORDER BY sku;","checks":[{"condition":"First basket has two different items","change_sql":"","expected":[["Boot",2,1],["Sock",1,1]],"observed":[["Boot",2,1],["Sock",1,1]]},{"condition":"Two rows of socks are still one buyer","change_sql":"INSERT INTO line_item VALUES ('1','Sock');","expected":[["Boot",2,1],["Sock",1,1]],"observed":[["Boot",2,1],["Sock",1,1]]},{"condition":"B purchases again","change_sql":"INSERT INTO purchase VALUES ('4','B','2026-03-01');","expected":[["Boot",2,2],["Sock",1,1]],"observed":[["Boot",2,2],["Sock",1,1]]}]}