KOEN
All notes
DATA & QUERIES

Revenue grew after the join

Joining synthetic orders to their items turned 300 into 400. Summing distinct amounts looked promising until two orders had the same price.

Threads linking two receipts to three item tiles beside a three-row paper table that repeats the green receipt

There were only two orders

Order A is $100 and order B is $200. These invented orders total $300. After attaching their item rows, summing the same order-total column returns $400.

No extra sale took place. We read another table.

The datapitfalls README argues for examining mistakes upstream of a chart, including question framing and data transformation. Its mention of joins multiplying rows prompted a small experiment.

I did not run the tool’s analysis engine. I put synthetic orders and item rows into an in-memory SQLite database, stored money as integer cents, and compared the totals. The figures below come from that isolated exercise.

Before looking at a finished chart, I wanted to find where the extra $100 appeared.

One order takes two seats

Order A has two item rows and B has one. Joining on order ID produces three combinations: A with its first item, A with its second, and B with its item.

The order total travels into each row: 100, 100, 200. Adding them yields 400. The aggregate has added the three quantities it received.

SQLite’s JOIN documentation describes retaining combinations that satisfy the join condition. Two matching item rows put A’s order information into two combinations. The join key does not have to be wrong for this to happen.

In the original table, a row represented an order. The joined result represents an order paired with an item row. A column still named order total makes the change easy to overlook.

Before labeling the result revenue, I would ask whether repeated amounts came from the same order. Rows added to show the items do not create more orders.

Removing equal amounts removes another order

Would summing unique values fix it? SUM(DISTINCT total_cents) returns $300 for the first fixture. The expected number is back.

Before accepting that result, I changed B to $100 too. Two orders now total $200. The same calculation returns $100.

SQLite documents this DISTINCT as filtering duplicate inputs to an aggregate. It cannot distinguish two separate orders that happen to cost the same. The different prices in the first fixture concealed the problem.

The intention was to count an order’s repeated amount once. But we need to retain separate orders even when their amounts match.

Two banknotes with the same face value are still two banknotes. Equal monetary values are not enough reason to merge the things carrying them.

Make one row per order first

For these fixtures, I grouped the item side by order ID first. Counting items still leaves one summary row per order. I then attached that result to the orders with a LEFT JOIN, assuming one source row per order ID.

The order totals now sum to $300 in the first fixture and $200 when both orders cost $100. The change removes the place where the order total was repeated across item rows.

This small example cannot supply the query for every revenue report. The data and question determine what counts as an order and which amount to sum. I checked the results of aligning the row units of these two invented tables.

When writing SQL, or asking AI to write it, I would state what one result row represents alongside the expected total. A query that runs without errors still lets this example’s $400 through.

I return to the result showing 400. Before adjusting decimal places or bar colors, I would unfold the order IDs beneath it. A has taken two seats.

Sources

All notes