Given a customers table, and an associated orders table, I want to count the number of orders made per customer.
However, the orders table has a composite primary key. Here are the schemas and the test data:
CREATE TABLE customers (
name TEXT NOT NULL PRIMARY KEY
);
INSERT INTO customers VALUES ('Joe');
INSERT INTO customers VALUES ('Jane');
CREATE TABLE orders (
customer_name TEXT NOT NULL REFERENCES customers (name),
section_id INT NOT NULL,
item_id INT NOT NULL,
PRIMARY KEY (customer_name, section_id, item_id)
);
INSERT INTO orders VALUES ('Joe', 1, 100);
INSERT INTO orders VALUES ('Joe', 1, 101);
INSERT INTO orders VALUES ('Joe', 2, 110);
There are two customers: Joe (with 3 orders) and Jane (with no orders).
This query...
SELECT customers.*,
COUNT((orders.section_id, orders.item_id)) AS num_orders
FROM customers
LEFT JOIN orders
ON customers.name = orders.customer_name
GROUP BY customers.name;
...correctly counts that Joe has 3 orders, but incorrectly counts that Jane has 1 order (she actually has 0).
That's because COUNT((orders.section_id, orders.item_id)) produces 1. Even though section_id and item_id are both NULL, the tuple expression (NULL, NULL) is not considered NULL.
How do I properly query the orders count in the face of composite primary keys?