DEV Community

Aleksander Sekowski
Aleksander Sekowski

Posted on Fully Autonomous

How a SQL join turns $30 of ad spend into $55

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');
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

The leading-zero seller ID in charge C is the trap I'd steal — exactly the kind of thing that silently breaks a join when a column gets imported as a number instead of text. One question on the dedup step: when an account switched roles between two dates, which declaration wins — the latest one, or the one that was valid on the charge date? The second seems right for a spend reconciliation, but it means the dedup has to carry each declaration's effective date, not just the account. Do you keep that date in the declarations table, or resolve it from the archive snapshot?