I hv two tables:-
First Table:-
EFF_DATE RATE CURRENCY TYPE
20110101 1.286 USD 1
20110101 1.300 USD 2
20110201 1.275 USD 1
20110201 1.290 USD 2
20110301 1.275 USD 1
20110301 1.285 USD 2
20110401 1.260 USD 1
20110401 1.270 USD 2
20110501 1.225 USD 1
20110501 1.230 USD 2
2nd Table:-
PO_NO TRANS_DATE ACCT_DATE SUPP_NO CURRENCY LOCAL_AMT
1000068 20110114 20110115 S016 USD 16,500.00
1000070 20110214 20110215 S016 USD 7,660.00
1000072 20110317 20110322 S025 USD 1,080.00
1000132 20110314 20110315 S037 USD 3,500.00
1000133 20110414 20110415 S038 USD 14,500.00
Based on ACCT_DATE & TYPE = 1, I wish to add a RATE column & the result should look like this:-
PO_NO TRANS_DATE ACCT_DATE SUPP_NO CURRENCY LOCAL_AMT RATE
1000068 20110114 20110115 S016 USD 16,500.00 1.286
1000070 20110214 20110215 S016 USD 7,660.00 1.275
1000072 20110317 20110322 S025 USD 1,080.00 1.275
1000132 20110314 20110315 S037 USD 3,500.00 1.275
1000133 20110414 20110415 S038 USD 14,500.00 1.26
1000170 20110531 20110531 S016 USD 15,400.00 1.225
I used the below SQL to run but for some reasons, it just keep looping without any output:-
SELECT st.*, ft.rate
FROM second_table st
LEFT JOIN first_table ft
ON (MONTH(acct_date) = MONTH(eff_date) AND YEAR(acct_date) = YEAR(eff_date) )
where ft.TYPE=1
Pls help.
Thanks.
Based on your comment above you need to put an index on ft.TYPE
You could also set up the query like this which might improve how the optimizer runs the query
Also, are the dates stored as strings or as datetime values. If they are stored as strings you should do a string compare instead of coverting to datetimes and then extracting the month and year.