0
votes

I have two parent child tables and a couple of supporting tables. I am struggling to figure out the right relationships between them.

  1. Orders, a table that holds a list of Orders including:Order Id, Supplier, Order Date etc..
  2. OrderDetails, a Table holds a list of products that have been ordered, and is a child of Orders, linked on OrderId. Key fields are the ProductId and the Quantity Ordered. Each order containes one or many OrderDetails that outline the products and quantities being ordered.
  3. Shipments, a table that holds a list of Shipments, including Tracking Number and Shipper. Links to Orders through Order Id. An Order may have multiple shipments. For example, I ordered 100 lightbulbs. They were dispatched in 5 shipments.
  4. ShipmentDetails, holds a list of product and shipped quantities and is linked to the Shipments table by ShippingId. Each Shipment may have multiple ShipmentDetails. i.e. One shipment may have 30 Lightbulbs and 10 Door Knobs.
  5. Products is a table that holds a list of Products that need to be both ordered and shipped.

So the logic is that I enter details about the products that are to be ordered in Products. For example, 100 CREE LED Lightbulbs, and 50 Door Handles.

When I place an order, I create an Order in Orders. i.e. amazon.com, order #45454.

Then I add child rows to that order in OrderDetails. i.e. 30 CREE LED Lightbulbs.

When the order Ships, I create an entry in the Shipments table. i.e. Tracking #46464646464, linked to OrderId in Orders. And then I enter what is in that shipment in ShipmentDetails. For example, only 20 or the 30 CREE LED Lightbulbs may be associated with this Shipping entry.

I am struggling with figuring out how to relate the Shipping Detail records to the Order Detail Table. Lets say the Shipping Detail Table has 4 fields.

  1. ShippingID - a link to the Shipments table parent.
  2. TrackingNum - a field that holds a tracking number.
  3. Product - this should be a drop down, that says, the shipment I belong to is related to a defined Order (because ShipmentDetail is a child of a Shipment record which in turn holds a key to the Orders table, which in turn has OrderDetails that reference products).
  4. Quantity - this should have a default (overridable) value that is the "Quantity Ordered" number from the OrdersDetail record that matches the Order Id and Product Id. Where OrderId is in the Shipments table (linked to the Orders table) and ProductID comes from #3 above. The reason it must be overridable is that a shipment may be a partial shipment.

I am stuck with the preceding #3 and #4. I hope I have explained this in a vaguely understandable way! The pictures below may help!

The ask:

  • What is the correct join between the ShipmentDetails and the OrderDetail Table
  • How do I create a field in the ShipmentDetail table with a default value pulled from the Quantities Field of the OrderDetail table, where

    Shipments!OrderId = Orders!Id and ShipmentDetail!ProductID = OrderDetails!Product ID
    

I am working in MS Access 2016 - but I suspect this is a fairly generic SQL question...rather than MS Access specific.

enter image description here enter image description here

enter image description here

1
Please pare down your question (i.e. make it smaller), and just show us the table definitions (one line per definition), maybe some sample data, and then the output you are trying to generate.Tim Biegeleisen

1 Answers

1
votes

I think the naming is confusing.

What I would rather have is (each first id is autoincrement, forgot how to say this on access):

// Product doesn't contain any information about quantity/price etc ...
create table Products(product_id int, name text, description text ...);

// Orders:
create table Orders(order_id int, client_name text, description text ...);
create table OrderDetails(order_detail_id int, order_id int, product_id int, quantity double, unit_price double, ...);

// Shipments
create table Shipments(shipment_id int, company text, description text ...);
create table ShipmentDetails(shipment_detail_id int, shipment_id int, product_id int, quantity double, price double default 0, ...);

// inserting shipments per product (defaulting to total quantity per product), assuming we have shipment_id SID
insert into ShipmentDetails(shipment_id, order_id, product_id, quantity)
select SID, order_id, product_id, SUM(quantity)
from OrderDetails
group by order_id, product_id;

Then you can of course have some filters (date, customer etc ...).

For the first question, I am not clear what exaclty you want to return.

Here is a comparison of quantities:

select t.order_id, t.product_id, sum(t.quantity) as product_quantity, sum(u.quantity) as shipment_quantity
from OrderDetails t inner join ShipmentDetails u 
on t.order_id = u.order_id and t.product_id = u.product_id;