Another_Henry
07-10-2003, 03:11 PM
Hi,
I'm trying to set up a simple invoicing system, and am having trouble figuring out the right query to retrieve data from three tables...
The important fields in the tables are:
Customers: CustomerID, Name, Address etc etc
Orders: OrderID, CustomerID, PaidSoFar
OrderRows: OrderID, ProductID, RowTotal
I need to make a list looking something like this:
CustomerID Name Money Owed
--------------------------------------------------
1111 Fred Enterprises 113.45
1112 Tim's Business 9296.50
1113 Fred Enterprises 0.00
The money owed for each order is found by summing the RowTotals for all the rows in that order, then subtracting that order's PaidSoFar.
Unfortunately I'm not sure how to do this with one query.
So far, I have:
SELECT Customers.CustomerID, Customers.Name, SUM( Orders.PaidSoFar ) Paid
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
GROUP BY Customers.CustomerID
to find the total that each customer has paid, and
SELECT Customers.CustomerID, SUM( OrderRows.RowTotal ) Total
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
INNER JOIN OrderRows ON Orders.OrderID = OrderRows.OrderID
GROUP BY Customers.CustomerID
to find how much they need to pay in total, but I can't combine them into one query to find the difference.
Thanks very much for any help you can give me!
I'm trying to set up a simple invoicing system, and am having trouble figuring out the right query to retrieve data from three tables...
The important fields in the tables are:
Customers: CustomerID, Name, Address etc etc
Orders: OrderID, CustomerID, PaidSoFar
OrderRows: OrderID, ProductID, RowTotal
I need to make a list looking something like this:
CustomerID Name Money Owed
--------------------------------------------------
1111 Fred Enterprises 113.45
1112 Tim's Business 9296.50
1113 Fred Enterprises 0.00
The money owed for each order is found by summing the RowTotals for all the rows in that order, then subtracting that order's PaidSoFar.
Unfortunately I'm not sure how to do this with one query.
So far, I have:
SELECT Customers.CustomerID, Customers.Name, SUM( Orders.PaidSoFar ) Paid
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
GROUP BY Customers.CustomerID
to find the total that each customer has paid, and
SELECT Customers.CustomerID, SUM( OrderRows.RowTotal ) Total
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
INNER JOIN OrderRows ON Orders.OrderID = OrderRows.OrderID
GROUP BY Customers.CustomerID
to find how much they need to pay in total, but I can't combine them into one query to find the difference.
Thanks very much for any help you can give me!