straatlang
Technical User
I hope somebody can help me with the next problem.
There are three tables involved:
Table: movements.
Value Date Account Amount Cur DRCR
1-1-2003 123 2000 EUR CR
1-2-2003 456 1500 EUR CR
1-2-2003 123 1450 USD CR
1-4-2003 456 1250 EUR CR
1-7-2003 123 800 USD CR
15-1-2003 123 950 EUR CR
20-1-2003 789 5000 USD CR
27-1-2003 456 5000 USD CR
28-1-2003 456 3000 USD CR
29-1-2003 123 10000 EUR DT
Table :names
Account Name
123 ABC
456 DEF
789 GHJ
Table :rates
CurID Erate RateDate
EUR 1 01-01-2003
USD 0.98 01-01-2003
GBP 0.65 02-01-2003
USD 0.95 02-01-2003
GBP 0.66 03-01-2003
USD 0.90 03-01-2003
GBP 0.34 03-01-2003
ect
The query should show the balances per account per currency per <=Valuedate. And the balance as
EUR but also the currency rate that is <= to the value date that I enter.
The query that I am using (thanks to Swampboogie) is almost correct but is not showing the correct
exchange rate.
Who can help me out.
The Query as it is now:
SELECT M.Account, Abs(Sum(Amount*IIf(DRCR='CR',1,-1))) AS Balance, M.Cur, IIf(Sum(Amount*IIf(DRCR='CR',1,-1))>0,'CR','DR') AS FLAG, [Enter Value Date] AS SelectedDate, R.Erate, N.Name, Erate*Balance AS V_EURO
FROM (Movements AS M INNER JOIN [Names] AS N ON M.Account=N.Account) INNER JOIN Rates AS R ON M.Cur=R.CurID
WHERE M.[Value Date] <= DateValue([Enter Value Date]) and R.rateDate = (select max(rateDate) from rates where curID = R.curID and rateDate <= M.[Value Date] )
GROUP BY M.Account, M.Cur, R.Erate, N.Name;
When I enter 31-1-2003 as the value date the result is then
Account Balance CurFLAG SelectedDate Erate Name V_EURO
123 7050 EUR DR 31-01-2003 1 ABC 7050
123 2250 USD CR 31-01-2003 0.98 ABC 2205
456 2750 EUR CR 31-01-2003 1 DEF 2750
456 8000 USD CR 31-01-2003 0.9 DEF 7200
789 5000 USD CR 31-01-2003 0.95 GHJ 4750
The erate is not "correct" what i want to see is the same rate in currencies
for all account namely the one <= to the value date.
EXpected result:
Account Balance CurFLAG SelectedDate Erate Name V_EURO
123 7050 EUR DR 31-01-2003 1 ABC 7050
123 2250 USD CR 31-01-2003 0.90 ABC 2025
456 2750 EUR CR 31-01-2003 1 DEF 2750
456 8000 USD CR 31-01-2003 0.90 DEF 7200
789 5000 USD CR 31-01-2003 0.90 GHJ 4275
I dont how to do this who can help me out.
Regards
Straatlang