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' )…