Thanks
MajP.
The SQL is so powerful. I thought we have to write some code to get this result but not, pure SQL also enough to do this.
I follow you and complete the Union query for my case as below:
...................
SELECT
tblLoanAmount.LoanAmount, tblLoanRates.RateStartRange, tblLoanRates.RateEndRange, tblLoanRates.RangeRate,
CLng([RateEndRange]-[LoanStartDate]) AS Rate_Days,
[Rate_Days]/365*[RangeRate]*[LoanAmount] AS Interest
FROM tblLoanAmount, tblLoanRates
WHERE
(((tblLoanRates.RateStartRange)<=[LoanStartDate]) AND ((tblLoanRates.RateEndRange)<=[LoanEndDate])) AND (CLng([RateEndRange]-[LoanStartDate]))>=0;
UNION
SELECT
tblLoanAmount.LoanAmount, tblLoanRates.RateStartRange, tblLoanRates.RateEndRange, tblLoanRates.RangeRate,
CLng([LoanEndDate]-[LoanStartDate]) AS Rate_Days,
[Rate_Days]/365*[RangeRate]*[LoanAmount] AS Interest
FROM tblLoanAmount, tblLoanRates
WHERE
(((tblLoanRates.RateStartRange)<=[LoanStartDate]) AND ((tblLoanRates.RateEndRange)>=[LoanEndDate]));
UNION
SELECT
tblLoanAmount.LoanAmount, tblLoanRates.RateStartRange, tblLoanRates.RateEndRange, tblLoanRates.RangeRate,
CLng([RateEndRange]-[rateStartRange]) AS Rate_Days,
[Rate_Days]/365*[RangeRate]*[LoanAmount] AS Interest
FROM tblLoanAmount, tblLoanRates
WHERE
(((tblLoanRates.RateStartRange)>=[LoanStartDate]) AND ((tblLoanRates.RateEndRange)<=[LoanEndDate]));
UNION SELECT
tblLoanAmount.LoanAmount, tblLoanRates.RateStartRange, tblLoanRates.RateEndRange, tblLoanRates.RangeRate,
CLng([LoanEndDate]-[RateStartRange]) AS Rate_Days,
[Rate_Days]/365*[RangeRate]*[LoanAmount] AS Interest
FROM tblLoanAmount, tblLoanRates
WHERE
(((tblLoanRates.RateStartRange)>=[LoanStartDate]) AND ((tblLoanRates.RateEndRange)>=[loanEndDate])) AND (CLng([CalcEndDate]-[RateStartRange]))>=0;
..................
- I add "AND (CLng([RateEndRange]-[LoanStartDate]))>=0" in order to remove the minus value from query.
So far is good but sorry that I was lack 1 more term in this case.
- Another term I lacked is the
Interest Calculating Date beside the Loan Start/End Date. The Date Start calculates interest maybe applied before the LoanStartDate and the Date End for calculating maybe after the LoanEndDate (Over due loan) . Here I call "CalcStartDate | CalcEndDate". The number of day Over Due Loan is [CalcEndDate]-[LoanEndDate] and these days will bear 50% of Interest from the RangeRate in its time range.
Ex: the Loan 1: 150,000,000; over due interest is 9% of range 08/17/2012 - 03/07/2013 for 62 days (from 03/03/2013 - 05/04/2013)
- I modified your query as below but still not get the result. So please help if you have free time.
Thank much for your spending time.
...........
SELECT
tblLoanAmount.LoanAmount, tblLoanRates.RateStartRange, tblLoanRates.RateEndRange, tblLoanRates.RangeRate,
IIF([RateStartRange]<[CalcStartDate],[CalcStartDate],[RateStartRange]) AS FromDate,
IIF([RateEndRange]>[CalcEndDate],[CalcEndDate],[RateEndRange]) AS ToDate,
CLng([RateEndRange]-[CalcStartDate]) AS Rate_Days,
[Rate_Days]/365*[RangeRate]*[LoanAmount] AS Interest
FROM tblLoanAmount, tblLoanRates
WHERE
(((tblLoanRates.RateStartRange)<=[CalcStartDate]) AND ((tblLoanRates.RateEndRange)<=[CalcEndDate])) AND (CLng([RateEndRange]-[CalcStartDate]))>=0;
UNION
SELECT
tblLoanAmount.LoanAmount, tblLoanRates.RateStartRange, tblLoanRates.RateEndRange, tblLoanRates.RangeRate,
IIF([RateStartRange]<[CalcStartDate],[CalcStartDate],[RateStartRange]) AS FromDate,
IIF([RateEndRange]>[CalcEndDate],[CalcEndDate],[RateEndRange]) AS ToDate,
CLng([CalcEndDate]-[CalcStartDate]) AS Rate_Days,
[Rate_Days]/365*[RangeRate]*[LoanAmount] AS Interest
FROM tblLoanAmount, tblLoanRates
WHERE
(((tblLoanRates.RateStartRange)<=[CalcStartDate]) AND ((tblLoanRates.RateEndRange)>=[CalcEndDate]));
UNION
SELECT
tblLoanAmount.LoanAmount, tblLoanRates.RateStartRange, tblLoanRates.RateEndRange, tblLoanRates.RangeRate,
IIF([RateStartRange]<[CalcStartDate],[CalcStartDate],[RateStartRange]) AS FromDate,
IIF([RateEndRange]>[CalcEndDate],[CalcEndDate],[RateEndRange]) AS ToDate,
CLng([RateEndRange]-[rateStartRange]) AS Rate_Days,
[Rate_Days]/365*[RangeRate]*[LoanAmount] AS Interest
FROM tblLoanAmount, tblLoanRates
WHERE
(((tblLoanRates.RateStartRange)>=[CalcStartDate]) AND ((tblLoanRates.RateEndRange)<=[CalcEndDate]));
UNION SELECT
tblLoanAmount.LoanAmount, tblLoanRates.RateStartRange, tblLoanRates.RateEndRange, tblLoanRates.RangeRate,
IIF([RateStartRange]<[CalcStartDate],[CalcStartDate],[RateStartRange]) AS FromDate,
IIF([RateEndRange]>[CalcEndDate],[CalcEndDate],[RateEndRange]) AS ToDate,
CLng([CalcEndDate]-[RateStartRange]) AS Rate_Days,
[Rate_Days]/365*[RangeRate]*[LoanAmount] AS Interest
FROM tblLoanAmount, tblLoanRates
WHERE
(((tblLoanRates.RateStartRange)>=[CalcStartDate]) AND ((tblLoanRates.RateEndRange)>=[CalcEndDate])) AND (CLng([CalcEndDate]-[RateStartRange]))>=0;
...........
Sample database:
Link