I have 3 tables: CUSTOMERS CLOSEDORDERS CLOSEDORDERARCHIVES
Each is linked by a customer ID field. CLOSEDORDERS and CLOSEDORDERSARCHIVES are tables with every line item of every invoice ever sold. I'm trying to come up with lifetime totals for the customers, but I'm stuck on the final sum. Here is what I have written, it outputs correct totals for each table in two columns (some of the values are null):
SELECT CUSTOMERS.Company, CUSTOMERS.[Ship City], CUSTOMERS.[Ship State],
(SELECT SUM (CLOSEDORDERSARCHIVE.Quantity*CLOSEDORDERSARCHIVE.SellPrice)
FROM CLOSEDORDERSARCHIVE
WHERE CLOSEDORDERSARCHIVE.CustomerID = CUSTOMERS.ID) AS archiveTotal,
(SELECT SUM (CLOSEDORDERS.Quantity*CLOSEDORDERS.SellPrice)
FROM CLOSEDORDERS
WHERE CLOSEDORDERS.CustomerID = CUSTOMERS.ID) AS currentTotal
FROM CUSTOMERS, CLOSEDORDERSARCHIVE, CLOSEDORDERS
WHERE (((CUSTOMERS.Branch)=33));
When I search, I find this answer that seems similar, but I can't make it work, is it because the sums are summed from line items in the tables and grouped by customer? Also, this is one of my first queries, is there a more efficient way to write/accomplish this?