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 |
|
JoshuaF
Starting Member
10 Posts |
Posted - 2005-11-24 : 03:51:47
|
I need to select amounts from an 'invoices' table (ID, InvoiceDate, Currency {USD,ZAR}, Amount) joined to another table that stores a list of currency rates 'CurrencyRates' (ID, RateDate, ConversionRate) and where it is a USD Currency, to show the relevant ConversionRate for that record.I tried thisSelect case I.Currency when 'USD' then CR.ConversionRate end from Invoices Ileft join CurrencyRates CR on CR.RateDate < I.InvoiceDatebut that gives me duplication for x number of records where x = total number of CurrencyRates records that have a RateDate < the Invoices InvoiceDateThen I tried this:Select case I.Currency when 'USD' then CR.ConversionRate end from Invoices Ileft join (Select top 1 * from CurrencyRates order by RateDate desc) CR on CR.RateDate < I.InvoiceDateThat showed the correct number of results, but that only showed the latest CurrencyRates record. I couldn't get it to show the top 1 relevant to the InvoiceDate.Does anyone have an easy solution?  |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-11-24 : 03:53:34
|
| Can you post some sample data from two tables and the result you want?MadhivananFailing to plan is Planning to fail |
 |
|
|
JoshuaF
Starting Member
10 Posts |
Posted - 2005-11-24 : 04:17:36
|
CurrencyRates TableID | RateDate | Rate50 | 2005-11-01 00:00:00.000 | 6.6000000049 | 2005-10-01 00:00:00.000 | 6.4000000047 | 2005-08-01 00:00:00.000 | 6.2000000045 | 2005-07-01 00:00:00.000 | 6.6000000043 | 2005-06-01 00:00:00.000 | 6.5500000042 | 2005-05-01 00:00:00.000 | 6.1000000034 | 2005-05-01 00:00:00.000 | 6.1000000041 | 2005-04-01 00:00:00.000 | 6.0000000040 | 2005-03-01 00:00:00.000 | 5.8500000039 | 2005-02-01 00:00:00.000 | 6.0000000038 | 2005-01-01 00:00:00.000 | 5.85000000Invoices TableID | InvoiceDate | Currency | Amount1 | 2005-07-01 15:36:00 | ZAR | 2940.002 | 2005-07-01 15:42:00 | ZAR | 20286.563 | 2005-07-01 15:57:00 | USD | 3700.004 | 2005-08-04 16:32:00 | ZAR | 20422.725 | 2005-08-05 09:13:00 | USD | 561.006 | 2005-09-10 09:04:00 | ZAR | 34176.467 | 2005-09-12 08:56:00 | ZAR | 42119.408 | 2005-09-12 09:54:00 | USD | 2400.009 | 2005-09-12 10:15:00 | USD | 2400.0010 | 2005-09-12 10:23:00 | USD | 1780.00I would like the result set to look like this:1 | 2005-07-01 15:36:00 | ZAR | 2940.00 2 | 2005-07-01 15:42:00 | ZAR | 20286.56 3 | 2005-07-01 15:57:00 | USD | 3700.00 | 6.600000004 | 2005-08-04 16:32:00 | ZAR | 20422.72 5 | 2005-08-05 09:13:00 | USD | 561.00 | 6.200000006 | 2005-09-10 09:04:00 | ZAR | 34176.46 7 | 2005-09-12 08:56:00 | ZAR | 42119.40 8 | 2005-09-12 09:54:00 | USD | 2400.00 | 6.400000009 | 2005-09-12 10:15:00 | USD | 2400.00 | 6.4000000010 | 2005-09-12 10:23:00 | USD | 1780.00 | 6.40000000Can you do it? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2005-11-24 : 04:37:19
|
Is this OK ?select [ID] , InvoiceDate , Currency , Amount, Rate = case when Currency = 'USD' then -- take the closest RateDate (select top 1 Rate from CurrencyRates x order by abs(datediff(day, x.RateDate, i.InvoiceDate))) endfrom Invoices i -----------------[KH] |
 |
|
|
JoshuaF
Starting Member
10 Posts |
Posted - 2005-11-24 : 05:51:03
|
That solved it totally!Thanks for saving me another 3 hours of hair pulling. |
 |
|
|
|
|
|
|
|