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 |
|
Gold999
Starting Member
6 Posts |
Posted - 2010-02-12 : 06:02:30
|
| Dear All,Please advise me how to write the following T-SQL query.I have the table Investments:Investments-----------Name Date Amount Currency John 12/2/2010 00:15 1000.00 EUROLinda 12/2/2010 01:30 1500.00 GBPJack 12/2/2010 02:30 1300.00 EUROAnd I have the tables of Currencies rates:Currencies----------Currency EffectiveDate RateInUSDEURO 12/2/2010 00:00 1.24EURO 12/2/2010 01:00 1.25EURO 12/2/2010 02:00 1.26GBP 12/2/2010 00:00 3.24GBP 12/2/2010 01:00 2.25GBP 12/2/2010 02:00 5.26 I need to write the T-SQL query to calculate amount of investments in USD depending on Rate at EffectiveDate.The output will be:Name, AmountInvestmentsInUsdFor example:John 1240 (1.24*1000)Linda 3375 (2.25*1500)Jack 1638 (1.26*1300)I've started to write the query, but it looks something wrong. (not working)select Name, Amount * RateInUSDfrom Investments i, Currencies cwhere i.Currency = c.Currencyand i.Date > (select min(EffectiveDate) from Currencies where EffectiveDate < c.EffectiveDate)Please advise.Thanks in advance! |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-02-12 : 06:25:58
|
| What if the effective rate changes more than once in a given hour?PBUH |
 |
|
|
Gold999
Starting Member
6 Posts |
Posted - 2010-02-12 : 06:33:59
|
quote: Originally posted by Idera What if the effective rate changes more than once in a given hour?PBUH
Yes, it can happen.But I think the "nature" of the T-SQL query will be the same I've provided. The point is I've missed something in join condition(s), as result the output is not correct. So, I need advise how to write the T-SQL query correct way. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-12 : 07:45:05
|
seems like thisif sql 2000SELECT i.Name,i.Amount * (SELECT TOP 1 RateInUSD FROM Currencies WHERE Currency=i.Currency AND EffectiveDate < i.Date ORDER BY EffectiveDate DESC)FROM Investments i if sql 2005SELECT i.Name,i.Amount * c.RateInUSD FROM Investments iCROSS APPLY (SELECT TOP 1 RateInUSD FROM Currencies WHERE Currency=i.Currency AND EffectiveDate < i.Date ORDER BY EffectiveDate DESC)c ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Gold999
Starting Member
6 Posts |
Posted - 2010-02-12 : 08:05:38
|
quote: Originally posted by visakh16 seems like thisif sql 2000SELECT i.Name,i.Amount * (SELECT TOP 1 RateInUSD FROM Currencies WHERE Currency=i.Currency AND EffectiveDate < i.Date ORDER BY EffectiveDate DESC)FROM Investments i if sql 2005SELECT i.Name,i.Amount * c.RateInUSD FROM Investments iCROSS APPLY (SELECT TOP 1 RateInUSD FROM Currencies WHERE Currency=i.Currency AND EffectiveDate < i.Date ORDER BY EffectiveDate DESC)c ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Thanks, it's very beautiful and good solutions.If it possible to write something like this too (more ANSI-style I mean)?select Name, Amount * RateInUSD, c.EffectiveDate, Datefrom Investments i, Currencies cwhere i.Currency = c.Currencyand i.Date > (select max(EffectiveDate) from Currencies where EffectiveDate < c.EffectiveDate and Currency = c.Currency ) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-12 : 08:10:00
|
| [code]select Name, Amount * RateInUSD,c.EffectiveDate,Datefrom Investments i, Currencies cwhere i.Currency = c.Currencyandc.EffectiveDate = (select max(EffectiveDate)from Currencieswhere Currency = c.Currencyand EffectiveDate < i.Date )[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Gold999
Starting Member
6 Posts |
Posted - 2010-02-12 : 08:20:42
|
quote: Originally posted by visakh16
select Name, Amount * RateInUSD,c.EffectiveDate,Datefrom Investments i, Currencies cwhere i.Currency = c.Currencyandc.EffectiveDate = (select max(EffectiveDate)from Currencieswhere Currency = c.Currencyand EffectiveDate < i.Date ) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Exactly what I've searched for!!!Many Thanks!!!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-12 : 08:23:29
|
welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|