Link to home
Start Free TrialLog in
Avatar of emi_sastra
emi_sastra

asked on

QUERY TABLE INTEREST

Hi All,

I have 2 tables :

TableDeposit :

1. AccountNo
2. InterestType
3. DepositAmount

TableInterest :

1. InterestType
2. DepositAmount1
3. InterestRate1
4. DepositAmount2
5. InterestRate2
6. DepositAmount3
7. InterestRate3
8. DepositAmount4
9. InterestRate4
10. InterestRate5

The DepositAmount at TableInterest is the limit of Deposit Amount for a certain Interest Rate.

How do I get the right Interest Rate from TableInterest by joining those tables ?

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of plummet
plummet
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of emi_sastra
emi_sastra

ASKER

Hi John,

I've not tried it, but I think it should work.

Thank you very much for your help.
No problem - let me know if it doesn't work as you want
Sure, thank you.
Structure is defined in the image might help you in easy way.
User generated image
DECLARE		@INTERESTTYPE CHAR(10)
SET			@INTERESTTYPE = 'INTY2';

WITH	LASTVISIT ( DEPOSITAMOUNT, INTERESTRATE )
AS
(
SELECT	DEPOSITAMOUNT,
		INTERESTRATE  
FROM	TABLEINTEREST 
WHERE	INTERESTTYPE = @INTERESTTYPE
)
SELECT	TOP 1 
		LV.INTERESTRATE
FROM	TABLEDEPOSIT TD
INNER 
JOIN	LASTVISIT LV
ON		TD.DEPOSITAMOUNT < LV.DEPOSITAMOUNT
ORDER 
BY		LV.DEPOSITAMOUNT;
GO

Open in new window

Data as shown in image User generated imageAnd output is based on interest type.
That's great.

I know the interest table is not normalized.
Since the structure is like that in dbf and I want to transfer to sql with the structure you suggest.

How could I do it ?

Should I open a new question for this ?

Thank you.
I  think so, you should really try to normalise the table as anuradhay suggests but that is really a different question.

Cheers
John
Let me open a new question.

Thank you.
Done.