Your select statement references fields for date and does some
renaming, which makes it difficult to write a precise query. However,
there are two key problems with your approach.
Firstly, you will find it difficult to do a summation of total paid
for a given customer and invoice in the same query. The easiest way
arround this is to build two seperate queries: one that summarizes
total payments by invoice and customer number, and a second that uses
the resut of the first query to reduce the original invoice payment.
So, using ONLY the fields listed above (add or rename as needed, it
will not affect the logic as long as you do not change your join
properties).
Query1 - saved as "first":
SELECT INV.[CUSTOMER NUMBER], INV.[INVOICE NUMBER], INV.AMOUNT,
Sum(INV_PAYMENT.CHECKAMOUNT) AS [Total Paid]
FROM INV LEFT JOIN INV_PAYMENT ON (INV.[CUSTOMER NUMBER] =
INV_PAYMENT.[CUSTOMER NUMBER]) AND (INV.[INVOICE NUMBER] =
INV_PAYMENT.[INVOICE NUMBER])
GROUP BY INV.[CUSTOMER NUMBER], INV.[INVOICE NUMBER], INV.AMOUNT;
Query2:
SELECT first.[CUSTOMER NUMBER], first.[INVOICE NUMBER], first.AMOUNT,
first.[Total Paid], [AMOUNT]-[Total Paid] AS Remainder
FROM [first];
Hope this will help! If you provide a complete list of the starting
fields in the tables and the exact output fields you want, the
researcher will be able to write the exact SQL syntax you require much
more easily. |