You run "revenue by category". The table looks fine. Then someone adds up the rows and gets twice your actual revenue.
Nothing errored. Every row is correct on its own. The total is still wrong, and the cause is one join.
Here is the whole problem in four products. Every query below was run on PostgreSQL 16, and the output is copied from that run.
The setup
CREATE TABLE products ( id int PRIMARY KEY, name text NOT NULL ); CREATE TABLE product_categories ( product_id int REFERENCES products(id), category text NOT NULL, PRIMARY KEY (product_id, category) ); CREATE TABLE order_items ( id int PRIMARY KEY, product_id int REFERENCES products(id), amount numeric(10,2) NOT NULL ); INSERT INTO products VALUES (1, 'Trail shoes'), (2, 'Rain jacket'), (3, 'Water bottle'), (4, 'Head torch'); INSERT INTO product_categories VALUES (1, 'Running'), (1, 'Hiking'), (2, 'Hiking'), (2, 'Sale'), (3, 'Running'), (3, 'Hiking'), (3, 'Sale'), (4, 'Hiking'); INSERT INTO order_items VALUES (1, 1, 120.00), (2, 1, 120.00), (3, 2, 200.00), (4, 3, 15.00), (5, 3, 15.00), (6, 3, 15.00), (7, 4, 40.00); A product can sit in more than one category. That is normal: trail shoes are for running and for hiking.
Real revenue:
SELECT sum(amount) AS total_revenue FROM order_items; total_revenue --------------- 525.00 The query everyone writes
SELECT pc.category, sum(oi.amount) AS revenue FROM order_items oi JOIN product_categories pc ON pc.product_id = oi.product_id GROUP BY pc.category ORDER BY revenue DESC; category | revenue ----------+--------- Hiking | 525.00 Running | 285.00 Sale | 245.00 Each row is true. Hiking products really did sell 525.00. But put a total under it:
SELECT coalesce(pc.category, 'TOTAL') AS category, sum(oi.amount) AS revenue FROM order_items oi JOIN product_categories pc ON pc.product_id = oi.product_id GROUP BY ROLLUP (pc.category) ORDER BY revenue DESC; category | revenue ----------+--------- TOTAL | 1055.00 Hiking | 525.00 Running | 285.00 Sale | 245.00 1055.00, against real revenue of 525.00. A pie chart built on this table would be wrong in every slice.
Why it happens
The join returns one row per order item per category. Count the rows before and after:
SELECT count(*) AS order_rows FROM order_items; SELECT count(*) AS joined_rows FROM order_items oi JOIN product_categories pc ON pc.product_id = oi.product_id; order_rows ------------ 7 joined_rows ------------- 16 Seven order items became sixteen rows. The water bottle is in three categories, so each of its sales appears three times.
This check is worth making a habit: count the rows before and after every join. If the number went up, something is being counted more than once.
To see which products multiply:
SELECT p.name, count(*) AS categories FROM products p JOIN product_categories pc ON pc.product_id = p.id GROUP BY p.name HAVING count(*) > 1 ORDER BY categories DESC, p.name; name | categories --------------+------------ Water bottle | 3 Rain jacket | 2 Trail shoes | 2 Three honest ways to report it
There is no single right fix. It depends on what the reader will do with the numbers.
1. One primary category per product. The totals add up, and each sale is counted once.
ALTER TABLE product_categories ADD COLUMN is_primary boolean NOT NULL DEFAULT false; UPDATE product_categories SET is_primary = true WHERE (product_id, category) IN ((1,'Running'), (2,'Hiking'), (3,'Running'), (4,'Hiking')); SELECT pc.category, sum(oi.amount) AS revenue FROM order_items oi JOIN product_categories pc ON pc.product_id = oi.product_id AND pc.is_primary GROUP BY pc.category ORDER BY revenue DESC; category | revenue ----------+--------- Running | 285.00 Hiking | 240.00 285.00 + 240.00 = 525.00. The cost: "Sale" disappears, because no product has it as its primary category.
2. Split each sale evenly across its categories. The totals still add up, and every category keeps a share.
SELECT pc.category, round(sum(oi.amount / n.cats), 2) AS revenue FROM order_items oi JOIN product_categories pc ON pc.product_id = oi.product_id JOIN (SELECT product_id, count(*) AS cats FROM product_categories GROUP BY product_id) n ON n.product_id = oi.product_id GROUP BY pc.category ORDER BY revenue DESC; category | revenue ----------+--------- Hiking | 275.00 Running | 135.00 Sale | 115.00 275.00 + 135.00 + 115.00 = 525.00. The cost: no category's number matches what its products really sold.
3. Keep the overlap and say so. Sometimes "how much did hiking products sell?" is the real question, and 525.00 is the right answer to it. Then report the overlapping figures, and never show a total or a pie chart next to them.
SELECT pc.category, sum(oi.amount) AS revenue, round(100.0 * sum(oi.amount) / (SELECT sum(amount) FROM order_items), 1) AS pct_of_total FROM order_items oi JOIN product_categories pc ON pc.product_id = oi.product_id GROUP BY pc.category ORDER BY revenue DESC; category | revenue | pct_of_total ----------+---------+-------------- Hiking | 525.00 | 100.0 Running | 285.00 | 54.3 Sale | 245.00 | 46.7 The percentages add up to 201.0. That is fine, as long as the report says the categories overlap.
A guard you can put in a test
SELECT (SELECT sum(amount) FROM order_items) AS true_total, sum(revenue) AS sum_of_categories, sum(revenue) > (SELECT sum(amount) FROM order_items) AS overlaps FROM (SELECT pc.category, sum(oi.amount) AS revenue FROM order_items oi JOIN product_categories pc ON pc.product_id = oi.product_id GROUP BY pc.category) t; true_total | sum_of_categories | overlaps ------------+-------------------+---------- 525.00 | 1055.00 | t If overlaps is true, the breakdown needs a note, or one of the fixes above.
A short checklist
- Count the rows before and after every join.
- Any join through a many-to-many table (tags, categories, labels, teams) can multiply your amounts.
- Decide which of the three reports you mean, and write it on the chart.
- Never put a total under a breakdown until you have checked it against the real total.
The same thing happens when the query is written for you. We asked FactQL's sample data, a film rental database, for the top 5 film categories by revenue. The answer came back with the query that ran, the five rows, and this line: "Many films belong to more than one category, so category figures overlap and add up to more than the total." Having the query next to the number is what makes a double count something you can see.
Where has a join doubled a number on you?

