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);