如何获得转换列值

时间:2017-03-27 02:02:45

标签: sql

我们的数据库只提供基于cad的汇率(fx / cad),但是,我需要使用基于usd的汇率(fx / usd);所以我想做这样的事情

FX_rate_cad_based / FXrate_usd_to_cad as spotrate

SELECT npv.ValDate, month(npv.ValDate) as Month, rtrim(npv.CurID) as CurID, (npv.SpotRate) as SpotRate_Cad  
 FROM DB.NPVPLFxRate as npv 
 WHERE (npv.ValDate<='2017-03-23')  
 AND (npv.ValDate>='2016-11-01')    
 LEFT JOIN  
 (SELECT npv.ValDate as usdValDate, (npv.SpotRate) as SpotRate_usd  
 FROM DB.NPVPLFxRate as npv 
 WHERE (npv.ValDate<='2017-03-23')  
 AND (npv.ValDate>='2016-11-01')    
 AND (npv.CurID = "usd"))  usdFX    
 ON npv.ValDate = usdFX.usdValDate  
 ORDER BY npv.ValDate, npv.CurID    

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT 
  cad.ValDate, 
  month(cad.ValDate) as Month, 
  rtrim(cad.CurID) as CurID, 
  cad.SpotRate as SpotRate_Cad,
  cad.SpotRate / usd.SpotRate as SpotRate_USD
FROM DB.NPVPLFxRate as cad 
JOIN DB.NPVPLFxRate as usd ON usd.ValDate = cad.ValDate
WHERE cad.ValDate between '2016-02-28' and '2016-03-02' and usd.CurID = 'usd'
ORDER BY cad.ValDate, cad.CurID 

我不确定您应该考虑LEFT JOIN,因为您的费率表应该包含每天的完整信息。