I'm trying to write a query that will pull a column from "Purchase Order Items" into my query, but I'm not sure how to link it. It has a column called "Product ID" that is the same value as the existing "Product ID" already in the query - so I'm trying to grab costs from the "Purchase Order Items" table but everything I've tried keeps saying no table found - so apparently a lookup table is not needed, and instead, I must link the table in the query somehow?
SELECT
"Invoices"."Invoice Number" "Invoice Number",
"Invoices"."Invoice Date" "Invoice Date",
"Sales Persons"."Name" "Sales Person",
"Invoices"."Ship Method" "Ship Method",
"Invoice Items".*,
if_Null(if_Null("Invoice Items"."Cost", ifNull("Sales Order Items"."Cost", "Items"."Purchase Price")), 0) as 'Item Cost',
(if_Null(if_Null("Invoice Items"."Cost", ifNull("Sales Order Items"."Cost", "Items"."Purchase Price")), 0) * "Invoice Items"."Quantity") as 'Total Line Cost',
"Invoice Items"."Total (BCY)" -(if_Null(if_Null("Invoice Items"."Cost", ifNull("Sales Order Items"."Cost", "Items"."Purchase Price")), 0) * "Invoice Items"."Quantity") as 'Invoice Line Profit',
"Items"."SKU" "SKU",
"Invoice Items"."Cost" "Invoice Items Cost",
"Sales Order Items"."Cost" "SO Items Cost",
"Items"."Purchase Price" "Items Price",
"Total Monthly Profit"."Total Month Profit" "Total Month Profit",
if("Total Monthly Profit"."Total Month Profit" < 8000, 0, if("Total Monthly Profit"."Total Month Profit" < 10000, .16, if("Total Monthly Profit"."Total Month Profit" < 15000, .18, if("Total Monthly Profit"."Total Month Profit" < 20000, .2, if("Total Monthly Profit"."Total Month Profit" < 25000, .21, if("Total Monthly Profit"."Total Month Profit" < 35000, .22, if("Total Monthly Profit"."Total Month Profit" < 45000, .23, if("Total Monthly Profit"."Total Month Profit" < 55000, .235, if("Total Monthly Profit"."Total Month Profit" < 65000, .24, if("Total Monthly Profit"."Total Month Profit" < 75000, .245, if("Total Monthly Profit"."Total Month Profit" >= 75000, .25, 0))))))))))) "Commission Rate",
("Invoice Items"."Total (BCY)" -(if_Null(if_Null("Invoice Items"."Cost", ifNull("Sales Order Items"."Cost", "Items"."Purchase Price")), 0) * "Invoice Items"."Quantity")) * (if("Total Monthly Profit"."Total Month Profit" < 8000, 0, if("Total Monthly Profit"."Total Month Profit" < 10000, .16, if("Total Monthly Profit"."Total Month Profit" < 15000, .18, if("Total Monthly Profit"."Total Month Profit" < 20000, .2, if("Total Monthly Profit"."Total Month Profit" < 25000, .21, if("Total Monthly Profit"."Total Month Profit" < 35000, .22, if("Total Monthly Profit"."Total Month Profit" < 45000, .23, if("Total Monthly Profit"."Total Month Profit" < 55000, .235, if("Total Monthly Profit"."Total Month Profit" < 65000, .24, if("Total Monthly Profit"."Total Month Profit" < 75000, .245, if("Total Monthly Profit"."Total Month Profit" >= 75000, .25, 0)))))))))))) "Commission Amount",
If((if_Null(if_Null("Invoice Items"."Cost", ifNull("Sales Order Items"."Cost", "Items"."Purchase Price")), 0) * "Invoice Items"."Quantity") = 0, 100, (if_Null(if_Null("Invoice Items"."Cost", ifNull("Sales Order Items"."Cost", "Items"."Purchase Price")), 0) * "Invoice Items"."Quantity")) 'Mindys Total Line Cost',
("Invoice Items"."Total (BCY)" -(if_Null(if_Null("Invoice Items"."Cost", ifNull("Sales Order Items"."Cost", "Items"."Purchase Price")), 0) * "Invoice Items"."Quantity")) / ((if_Null(if_Null("Invoice Items"."Cost", ifNull("Sales Order Items"."Cost", "Items"."Purchase Price")), 0) * "Invoice Items"."Quantity")) * 100 as 'Profit 1',
(If((if_Null(if_Null("Invoice Items"."Cost", ifNull("Sales Order Items"."Cost", "Items"."Purchase Price")), 0) * "Invoice Items"."Quantity") = 0, 1, (if_Null(if_Null("Invoice Items"."Cost", ifNull("Sales Order Items"."Cost", "Items"."Purchase Price")), 0) * "Invoice Items"."Quantity"))) * 100 as 'For calculation',
(If((if_Null(if_Null("Invoice Items"."Cost", ifNull("Sales Order Items"."Cost", "Items"."Purchase Price")), 0) * "Invoice Items"."Quantity") = 0, 100, (("Invoice Items"."Total (BCY)" -(if_Null(if_Null("Invoice Items"."Cost", ifNull("Sales Order Items"."Cost", "Items"."Purchase Price")), 0) * "Invoice Items"."Quantity")) / ((if_Null(if_Null("Invoice Items"."Cost", ifNull("Sales Order Items"."Cost", "Items"."Purchase Price")), 0) * "Invoice Items"."Quantity")) * 100))) as 'Final Profit % per Invoice'
FROM "Invoice Items"
LEFT JOIN "Invoices" ON "Invoices"."Invoice ID" = "Invoice Items"."Invoice ID"
LEFT JOIN "Sales Persons" ON "Sales Persons"."Sales Person ID" = "Invoices"."Sales Person ID"
LEFT JOIN "Items" ON "Items"."Item ID" = "Invoice Items"."Product ID"
LEFT JOIN "Sales Order Items" ON "Sales Order Items"."Item ID" = "Invoice Items"."SO ItemID"
LEFT JOIN "Total Monthly Profit" ON "Total Monthly Profit"."Salesperson" = "Sales Persons"."Name"
AND "Total Monthly Profit"."Month/Year" = date_format("Invoices"."Invoice Date", '%m/%Y')