0

I am trying to write an SQL query that would get the exchange rate for say Sterling to Euro.

I have tried the SQL below:

SELECT CurrencyCode,ExchangeRate FROM Currency
WHERE CurrencyCode='GBP';

I was expecting to get a list of two columns, currency name and exchange rates for sterling, but something is not right; any ideas?

So...

SELECT ExchangeRate FROM Currency WHERE CurrencyCode = 'EUR';  

This would return a single column for all of the rates for EUR against other currencies.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
David Cunningham
  • 957
  • 2
  • 12
  • 22

1 Answers1

0

Presuming you don't have an intermediate ExchangeRates table, so you're using a base currency instead, if the base currency is GBP, the query you want is:

SELECT ExchangeRate FROM Currency WHERE CurrencyCode = 'EUR';

Here's a demo.

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
  • Can you give me an example of how one would get the rate between Sterling (base) and Euro (target). – David Cunningham Dec 07 '13 at 22:34
  • @DavidCunningham That's actually the example I gave. Or do you mean that you have *multiple* base currencies in your table? – Danny Beckett Dec 07 '13 at 22:35
  • Yes sorry I have multiple base currencies – David Cunningham Dec 07 '13 at 22:39
  • Ok, so presumably you have another column saying which `CurrencyCode` each `CurrencyCode` references? The query would be something like `SELECT ExchangeRate FROM Currency WHERE CurrencyCode = 'EUR' and BaseCurrency = 'GBP';`. If you still can't get this working, please edit the demo SQLFiddle I posted to include your actual schema and some data; and post the link in a comment. – Danny Beckett Dec 07 '13 at 23:07