{"publisher":"WESWOO","id":"duplicate-profile-sales","engine":"SQLite","title":{"zh":"查看样例记录与复核查询","en":"Inspect sample records and query"},"setup_sql":"CREATE TABLE sale(id TEXT PRIMARY KEY,profile TEXT,amount INT);\nINSERT INTO sale VALUES ('1','A',5000),('2','B',3000),('3','C',2000);\nCREATE TABLE confirmed(profile TEXT,person TEXT,PRIMARY KEY(profile,person));\nINSERT INTO confirmed VALUES ('A','P1'),('B','P1');","query_sql":"WITH keymap AS (SELECT profile,COUNT(*) AS n,MIN(person) AS person FROM confirmed GROUP BY profile),\nlabeled AS (SELECT s.*,CASE WHEN n=1 THEN 'person:'||person WHEN n>1 THEN 'review:'||s.profile ELSE 'profile:'||s.profile END AS group_key\n FROM sale s LEFT JOIN keymap m ON m.profile=s.profile)\nSELECT group_key,COUNT(*),SUM(amount) FROM labeled GROUP BY group_key ORDER BY group_key;","checks":[{"condition":"Same confirmed buyer A and B; C shares an address only","change_sql":"","expected":[["person:P1",2,8000],["profile:C",1,2000]],"observed":[["person:P1",2,8000],["profile:C",1,2000]]},{"condition":"B is linked to two different people","change_sql":"INSERT INTO confirmed VALUES ('B','P2');","expected":[["person:P1",1,5000],["profile:C",1,2000],["review:B",1,3000]],"observed":[["person:P1",1,5000],["profile:C",1,2000],["review:B",1,3000]]},{"condition":"B confirmation withdrawn","change_sql":"DELETE FROM confirmed WHERE profile='B';","expected":[["person:P1",1,5000],["profile:B",1,3000],["profile:C",1,2000]],"observed":[["person:P1",1,5000],["profile:B",1,3000],["profile:C",1,2000]]},{"condition":"C confirmed separately","change_sql":"INSERT INTO confirmed VALUES ('C','P3');","expected":[["person:P1",2,8000],["person:P3",1,2000]],"observed":[["person:P1",2,8000],["person:P3",1,2000]]}]}