SQL JOIN 如何把 $30 廣告支出算成 $55
How a SQL join turns $30 of ad spend into $55
文章示範 SQL JOIN 如何因授權聲明重複而重複計算交易,令三筆合共 $30 的廣告支出在報表中變成 $55。案例使用 Sling 的 app-ads.txt 中 PubMatic 帳戶 163623 的 DIRECT 和 RESELLER 兩條聲明;交易金額是作者虛構的重現資料。
Three invoice rows add up to $30. Join them to a publisher’s list of authorized sellers and the dashboard says $55. Both tables can be correct. The bug is that the query lets the number of matching declarations decide how many times to count a purchase.
A November 25, 2026 snapshot supplies a real test case. Sling’s app-ads.txt listed PubMatic account 163623 on consecutive lines, once as DIRECT and once as RESELLER. PubMatic’s registry identified the account as Spacemob and gave it the seller type BOTH. I maintain the archive used for this check. The purchases below are invented so you can reproduce the bug locally. Sling’s declaration · PubMatic’s registry · Dated source evidence, including lines 41 and 42
Reproduce it in SQLite
Run the snippets in order in one SQLite session. All amounts are USD cents. The third charge uses a deliberately different seller ID with a leading zero. Keep that column as text throughout the import.
CREATE TEMP TABLE charges (
charge_id TEXT PRIMARY KEY, store TEXT, exchange TEXT,
seller_id TEXT, cents INTEGER
);
CREATE TEMP TABLE declarations (
store TEXT, exchange TEXT, seller_id TEXT, role TEXT
);
INSERT INTO charges VALUES
('A', 'apple:us:945077360', 'pubmatic.com', '163623', 1250),
('B', 'apple:us:945077360', 'pubmatic.com', '163623', 1250),
('C', 'apple:us:945077360', 'pubmatic.com', '0163623', 500);
INSERT INTO declarations VALUES
('apple:us:945077360', 'pubmatic.com', '163623', 'DIRECT'),
('apple:us:945077360', 'pubmatic.com', '163623', 'RESELLER');
The overcount comes from a straightforward left join.
SELECT COUNT(*) AS joined_rows, SUM(cents) AS joined_cents
FROM charges
LEFT JOIN declarations USING (store, exchange, seller_id);
It returns 5 rows and 5,500 cents. A matches two declarations and contributes 2,500 cents. B does the same. C has no match in this fixture and contributes its original 500 cents. Changing the expression to SUM(DISTINCT cents) gives 1,750 cents. Two legitimate purchases happen to cost the same, so SQLite drops one of them.
Give each account one evidence row
I group the declarations first. This keeps both roles available for review and gives each charge at most one row to join.
CREATE TEMP VIEW charge_review AS
WITH by_account AS (
SELECT store, exchange, seller_id, COUNT(*) AS declaration_lines,
MAX(role = 'DIRECT') AS has_direct,
MAX(role = 'RESELLER') AS has_reseller
FROM declarations
GROUP BY store, exchange, seller_id
)
SELECT c.*, COALESCE(d.declaration_lines, 0) AS declaration_lines,
d.has_direct, d.has_reseller
FROM charges AS c
LEFT JOIN by_account AS d USING (store, exchange, seller_id);
SELECT charge_id, cents, declaration_lines
FROM charge_review ORDER BY charge_id;
Now A and B each show 1,250 cents and two declaration lines. C shows 500 cents and zero lines in the fixture. The result tells me which purchase to investigate without choosing a selling relationship on the buyer’s behalf.
Put the invariant in a test
SELECT (SELECT COUNT(*) FROM charge_review) =
(SELECT COUNT(*) FROM charges) AS row_count_ok,
(SELECT SUM(cents) FROM charge_review) =
(SELECT SUM(cents) FROM charges) AS spend_ok;
Both results should be 1. In production I’d run the spend assertion per currency and reconcile on a unique invoice-line key. I’d also select the declaration snapshot for the period under review before grouping. Otherwise an account that switched roles between two dates can appear to have declared both at the same time.
For the real Sling account, the two lines tell me what to ask next. I’d request the seller chain for the sampled transaction and confirmation of who controlled the account on that route. The corrected dashboard still has three charges totaling $30.
This article was written with an AI agent. The SQL examples were executed in SQLite and the seller details were checked against the dated source records.
來源:DEV Community · dev.to