I have two tables in a this is Postgres database representing simple orders from a market. A master table with information about the order, and a detail table with containing specifics of the purchase, with a foreign key back to master. Easy enough.
Over thousands of orders from the market, I'd like to find some specific orders based on what was purchased and in what quantity.
I have two more tables, in similar fashion, a master and a child where I create a "pack" and detail items from the market.
For example: Pack A contains 2 Apples and 3 Oranges. I define that in the tables. Now I'd like to find how many orders, and which orders from the market match that specific combination exactly.
It's important that it is an exact match. An order containing additional products or with any different quantity does not match.
In the SQL Fiddle, I've setup the simple example with data. The raw DDL is below. Two of the orders in the table should match Pack A.
http://sqlfiddle.com/#!17/b4f55
CREATE TABLE customer_order(
order_id serial PRIMARY KEY NOT NULL,
customer_name VARCHAR(100) NOT NULL
);
CREATE TABLE order_detail(
id serial PRIMARY KEY,
order_id INTEGER,
item_sku VARCHAR(50),
item_quantity INTEGER,
FOREIGN KEY(order_id) REFERENCES customer_order(order_id)
);
INSERT INTO customer_order (customer_name) VALUES ('John');
INSERT INTO customer_order (customer_name) VALUES ('Mary');
INSERT INTO customer_order (customer_name) VALUES ('Bill');
INSERT INTO order_detail (order_id, item_sku, item_quantity) VALUES (1, 'APPLE', 2);
INSERT INTO order_detail (order_id, item_sku, item_quantity) VALUES (1, 'ORANGE', 3);
INSERT INTO order_detail (order_id, item_sku, item_quantity) VALUES (2, 'ORANGE', 5);
INSERT INTO order_detail (order_id, item_sku, item_quantity) VALUES (3, 'APPLE', 2);
INSERT INTO order_detail (order_id, item_sku, item_quantity) VALUES (3, 'ORANGE', 3);
CREATE TABLE pack_master(
pack_id serial PRIMARY KEY NOT NULL,
name VARCHAR(100) NOT NULL
);
CREATE TABLE pack_child(
id serial PRIMARY KEY,
pack_id INTEGER,
item_sku VARCHAR(50),
item_quantity INTEGER,
FOREIGN KEY(pack_id) REFERENCES pack_master(pack_id)
);
INSERT INTO pack_master (name) VALUES ('Pack A');
INSERT INTO pack_master (name) VALUES ('Pack B');
INSERT INTO pack_child (pack_id, item_sku, item_quantity) VALUES (1, 'APPLE', 2);
INSERT INTO pack_child (pack_id, item_sku, item_quantity) VALUES (1, 'ORANGE', 3);
INSERT INTO pack_child (pack_id, item_sku, item_quantity) VALUES (2, 'GRAPES', 5);
Assuming that
pack_child (pack_id, item_sku)
, as well asorder_detail (order_id, item_sku)
are definedUNIQUE
, this would work:Returns all pairs of
pack_id
andorder_id
that match exactly.db<>fiddle here
There are a hundred-and-one alternative ways to write the query. Which is the fastest depends on cardinalities, data distribution, constraints and, most importantly, available indexes.
It's a special application of relational-division. Here is an arsenal of techniques:
One alternative, probably faster: create views or materialized views of the parent tables including the item count:
(Orders typically don't change later, so might be viable candidates for a materialized view.)
Only if there can be many order items, an index might pay (index expressions in this order):
The query now only considers orders with a matching item count to begin with:
.. then, if all items match, we don't have to rule out additional items any more. And we have all columns from the parent table at our disposal right away, to return whatever it is you want to return ...