Revenue by Customer
Customer Revenue Details
| ID | Customer | Region | Revenue | Orders |
|---|
This query uses LEFT JOIN + GROUP BY + SUM -- aggregate functions that
CDC tools (Debezium, Supabase, ElectricSQL) and Hasura cannot express.
pg_reactive recomputes the aggregate on every DML and delivers the exact delta.
Customers with zero orders still appear thanks to the LEFT JOIN.
-- Connect to the database
-- docker compose exec -T db psql -U postgres -d pg_reactive_test
-- Big order for Alice (her bar grows)
INSERT INTO orders (customer_id, product_id, qty, total)
VALUES (1, 3, 5, 1749.95);
-- First order for Dave (bar appears from zero)
INSERT INTO orders (customer_id, product_id, qty, total)
VALUES (4, 1, 10, 799.90);
-- New customer appears with $0 (LEFT JOIN)
INSERT INTO customers (name, region)
VALUES ('Eve', 'eu-west');
-- Delete an order (revenue decreases)
DELETE FROM orders
WHERE customer_id = 3 AND total = 349.99;