After following all the mandatory steps in my "before posting steplist" (research, experiment, further research, frustration, further experiment, utter failure), I decided I'd ask you guys.
The task at hand is the following:
table1 AS t1 has the presWorth (currency) field;
table2 AS t2 has the payAmount (currency) field;
They both have fkCustID to relate records between each other.
I'm looking for a query that will return a calculated field with the result of the subtraction of t2.payAmount from t1.presWorth; if t1.presWorth goes down to zero, it will lookup presWorth in another record (the routine will always check if SUM(presValue) is >= to SUM(payAmount) and prompt the user to do other things if said condition is not met).
An example to break it down:
2 records in t1 each with presWorth of € 45
1 record in t2 with payAmount of 89
the query should return something like
1.'Prescription Residue' = € 1
2.'Prescription Residue' = € 0
What I have so far:
Nothing worthwhile, my attempt at the SQL is pathetic but here you go
The task at hand is the following:
table1 AS t1 has the presWorth (currency) field;
table2 AS t2 has the payAmount (currency) field;
They both have fkCustID to relate records between each other.
I'm looking for a query that will return a calculated field with the result of the subtraction of t2.payAmount from t1.presWorth; if t1.presWorth goes down to zero, it will lookup presWorth in another record (the routine will always check if SUM(presValue) is >= to SUM(payAmount) and prompt the user to do other things if said condition is not met).
An example to break it down:
2 records in t1 each with presWorth of € 45
1 record in t2 with payAmount of 89
the query should return something like
1.'Prescription Residue' = € 1
2.'Prescription Residue' = € 0
What I have so far:
Nothing worthwhile, my attempt at the SQL is pathetic but here you go
Code:
SELECT t1.presID, t1.presMonth+'-'+t1.presYear, t1.pres, t1.DataAdded,
(SELECT SUM(t1.presWorth-t2.payAmount) FROM Cust_Pres AS t1, Payments AS t2 WHERE t1.presID <= t1.presID AND t1.custID =t2.custID) AS 'Prescription Residue'
FROM Cust_Pres AS t1
WHERE t1.custID = x -- x is fed by VBA
ORDER BY t1.DataAdded DESC;