Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
man889
Starting Member
25 Posts |
Posted - 2011-04-14 : 23:03:03
|
| I have an invoice table and a currency rate table as following. I need to create a report convert the currency to USD or other currency. It is easy for me to convert it to USD, because the rate is base on USD in currency table. If I want to change to other currency, can I do that by SQL only without programming?Invoice table - sample datainvId, invDate, currencyName, price001, 1/1/2010, EUR, 10002, 1/1/2010, HKD, 100003, 1/1/2010, USD, 100004, 2/1/2010, EUR, 20005, 2/1/2010, HKD, 200006, 2/1/2010, USD, 200Currency table - sample datacurDate, currencyName, rate_1_usd, rate1/1/2010, EUR, 0.7018, 1.4249071/1/2010, HKD, 7.7545, 0.1289571/1/2010, USD, 1, 12/1/2010, EUR, 0.7118, 1.4349072/1/2010, HKD, 7.7145, 0.1389572/1/2010, USD, 1, 1SELECT invoice.invId, invoice.invDate, invoice.curName, invoice.price, [currency].currencyName, [currency].rate, invoice.price * [currency].rate AS price_USDFROM (invoice INNER JOIN [currency] ON invoice.invDate = [currency].valid_date AND invoice.curName = [currency].currencyName)ORDER BY invoice.invIdOutput1 1/1/2010 12:00:00 AM EUR 10 EUR 1.424907 14.2490702 1/1/2010 12:00:00 AM HKD 50 HKD 0.128957 6.4478503 1/1/2010 12:00:00 AM USD 100 USD 1.000000 100.0000004 2/1/2010 12:00:00 AM EUR 10000 EUR 1.524907 15249.0700005 2/1/2010 12:00:00 AM HKD 50000 HKD 0.158957 7947.8500006 2/1/2010 12:00:00 AM USD 80000 USD 1.000000 80000.000000 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-04-14 : 23:17:51
|
1st convert to your base and then convert to that curency.Example :SELECT invoice.invId, invoice.invDate, invoice.curName, invoice.price, [currency].currencyName, [currency].rate, invoice.price * [currency].rate AS price_USD, invoice.price * [currency].rate / euro.rate AS price_EURFROM invoice INNER JOIN [currency] ON invoice.invDate = [currency].valid_date AND invoice.curName = [currency].currencyName INNER JOIN [currency] euro ON invoice.invDate = [euro].valid_date AND [euro].currencyName = 'EUR'ORDER BY invoice.invId KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
man889
Starting Member
25 Posts |
Posted - 2011-04-14 : 23:32:57
|
quote: Originally posted by khtan 1st convert to your base and then convert to that curency.Example :SELECT invoice.invId, invoice.invDate, invoice.curName, invoice.price, [currency].currencyName, [currency].rate, invoice.price * [currency].rate AS price_USD, invoice.price * [currency].rate / euro.rate AS price_EURFROM invoice INNER JOIN [currency] ON invoice.invDate = [currency].valid_date AND invoice.curName = [currency].currencyName INNER JOIN [currency] euro ON invoice.invDate = [euro].valid_date AND [euro].currencyName = 'EUR'ORDER BY invoice.invId KH[spoiler]Time is always against us[/spoiler]
Hi khtan,Thank very much for your reply..Can you explain this part? I don't understand it and run sql successfully.INNER JOIN [currency] euro ON invoice.invDate = [euro].valid_dateAND [euro].currencyName = 'EUR'Thank you very much |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-04-14 : 23:36:36
|
that is the example if you want to convert to EUR.If you want to convert to another other currency just change the part in red to the required currencyAND [euro].currencyName = 'EUR' KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
man889
Starting Member
25 Posts |
Posted - 2011-04-14 : 23:49:23
|
| when i try to run it,it show that "??????"in English means "expression not support"I run the sql the Visual Studio Query Builder, and access 2000Thank very much |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-04-14 : 23:52:48
|
Firstly, you have posting in a SQL Server forum. There is a separate Access forum on this site.Second, i am not familiar with Access. Does Access support table alias ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
man889
Starting Member
25 Posts |
Posted - 2011-04-14 : 23:55:23
|
| Hi khtan,INNER JOIN [currency] euro ON invoice.invDate = [euro].valid_dateAND [euro].currencyName = 'EUR'I don't understand euro part? Is it like a temp view? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-04-14 : 23:56:23
|
that is the table aliasThe query that i posted is meant for SQL Server. As mention earlier, I am not familiar with Access so not sure how will Access take it. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
man889
Starting Member
25 Posts |
Posted - 2011-04-15 : 00:01:35
|
| Hi khtan,I will check the table alias.I think it is what I looking for.Thank you very much. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-04-15 : 00:04:55
|
maybe you will need the AS keyword. For SQL Server this is optional TryINNER JOIN [currency] AS euro ON invoice.invDate = [euro].valid_dateAND [euro].currencyName = 'EUR' KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-04-15 : 05:24:10
|
use ANSI JOIN. It is easier to read and debugFROM unacom1 inner join unafin1 on unafin1.pol_num = unacom1.pol_num and unafin1.pol_uw_yr = unacom1.pol_uw_yr and unafin1.endt_num = unacom1.endt_num inner join rvfcur on unafin1.prem_cur = rvfcur.currency and datepart(month, unacom1.close_date) = CONVERT(int, rvfcur.valid_mth) abd datepart(year, unacom1.close_date) = CONVERT(int, rvfcur.valid_year) inner join rvfcur as euro on datepart(month, unacom1.close_date) = CONVERT(int, euro.valid_mth) and datepart(year, unacom1.close_date) = CONVERT(int, euro.valid_year) and euro.currency = 'EUR'WHERE unacom1.close_date between '01JAN2010' and '02JAN2010' KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
man889
Starting Member
25 Posts |
Posted - 2011-04-15 : 05:48:09
|
| Hi khtan,You solved it.You are expert.Thank you very much. |
 |
|
|
|
|
|
|
|