简体   繁体   中英

Divide 2 columns with a WHERE clause

Requirement:

Divide EUR exchange rate by USD exchange rate and show result in a new column. For example, 0.912/0.822 = 1.1. I want the 1.1 in a new column called 'EUR/USD'

I will have to do this division for a few different rates but we can use the EUR/USD as an example.

Query

select er.Rate, wc.CurrencyCode + ' ' + wc.Descr as [Currency], er.EDate as [Exchange Date], ISNULL(wc.MonSymbol, ' ') AS [Symbol], er2.Rate as [Rate2]
from ExchangeRate er
JOIN WorldCurrency wc on er.ExCurrencyId = WC.InCurrencyId
JOIN ExchangeRate er2 on er.InExchangeRateId = er2.InExchangeRateId
inner join (
    select wc.CurrencyCode, max(er.EDate) as MaxDate
    from ExchangeRate er
    JOIN WorldCurrency wc on er.ExCurrencyId = WC.InCurrencyId
    group by wc.CurrencyCode
) erm on wc.CurrencyCode = erm.CurrencyCode and er.EDate = erm.MaxDate

Current Result

在此处输入图片说明

Schema

Attempt

select b.*,max(case when b.currency='EUR Euro' then b.Rate end)/max(case when 

     b.currency='USD US Dollar' then b.Rate end) as [EUR/USD] from

    (select er.Rate as Rate, wc.CurrencyCode + ' ' + wc.Descr as [Currency], er.EDate 
    as [Exchange Date], ISNULL(wc.MonSymbol, ' ') AS [Symbol], er2.Rate as [Rate2]
    from ExchangeRate er
    JOIN WorldCurrency wc on er.ExCurrencyId = WC.InCurrencyId
      JOIN ExchangeRate er2 on er.InExchangeRateId = er2.InExchangeRateId
  inner join (
     select wc.CurrencyCode, max(er.EDate) as MaxDate
       from ExchangeRate er
       JOIN WorldCurrency wc on er.ExCurrencyId = WC.InCurrencyId
     group by wc.CurrencyCode
    ) erm on wc.CurrencyCode = erm.CurrencyCode and er.EDate = erm.MaxDate)b
    group by b.rate, b.Currency, b.[Exchange Date], b.Symbol, b.Rate2

Result:

在此处输入图片说明

Solution:

select er.Rate, wc.CurrencyCode + ' ' + wc.Descr as [Currency], er.EDate as [Exchange Date], ISNULL(wc.MonSymbol, ' ') AS [Symbol],
[eur].Rate/[usd].rate as [EUR/USD], [usd].Rate/[zar].rate as [USD/ZAR], [eur].Rate/[zar].rate as [EUR/ZAR], [eur].Rate/[nok].rate as [EUR/NOK],
[eur].Rate/[bwp].rate as [EUR/BWP], [bwp].Rate/[zar].rate as [ZAR/BWP]
from ExchangeRate er
JOIN WorldCurrency wc on er.ExCurrencyId = WC.InCurrencyId
inner join (
    select wc.CurrencyCode, max(er.EDate) as MaxDate
    from ExchangeRate er
    JOIN WorldCurrency wc on er.ExCurrencyId = WC.InCurrencyId
    group by wc.CurrencyCode
) erm on wc.CurrencyCode = erm.CurrencyCode and er.EDate = erm.MaxDate
OUTER APPLY
(SELECT er3.Rate, wc3.CurrencyCode + ' ' + wc3.Descr as [Currency], er3.EDate as [Exchange Date]
FROM ExchangeRate er3
JOIN WorldCurrency wc3 on er3.ExCurrencyId = WC3.InCurrencyId
WHERE wc3.CurrencyCode = 'USD' AND er3.EDate = erm.MaxDate) as [USD]

OUTER APPLY
(SELECT er4.Rate, wc4.CurrencyCode + ' ' + wc4.Descr as [Currency], er4.EDate as [Exchange Date]
FROM ExchangeRate er4
JOIN WorldCurrency wc4 on er4.ExCurrencyId = WC4.InCurrencyId
WHERE wc4.CurrencyCode = 'EUR' AND er4.EDate = erm.MaxDate) as [EUR]

OUTER APPLY
(SELECT er5.Rate, wc5.CurrencyCode + ' ' + wc5.Descr as [Currency], er5.EDate as [Exchange Date]
FROM ExchangeRate er5
JOIN WorldCurrency wc5 on er5.ExCurrencyId = WC5.InCurrencyId
WHERE wc5.CurrencyCode = 'ZAR' AND er5.EDate = erm.MaxDate) as [ZAR]

OUTER APPLY
(SELECT er6.Rate, wc6.CurrencyCode + ' ' + wc6.Descr as [Currency], er6.EDate as [Exchange Date]
FROM ExchangeRate er6
JOIN WorldCurrency wc6 on er6.ExCurrencyId = WC6.InCurrencyId
WHERE wc6.CurrencyCode = 'NOK' AND er6.EDate = erm.MaxDate) as [NOK]

OUTER APPLY
(SELECT er7.Rate, wc7.CurrencyCode + ' ' + wc7.Descr as [Currency], er7.EDate as [Exchange Date]
FROM ExchangeRate er7
JOIN WorldCurrency wc7 on er7.ExCurrencyId = WC7.InCurrencyId
WHERE wc7.CurrencyCode = 'BWP' AND er7.EDate = erm.MaxDate

) as [BWP]

try this

    select b.*,case when b.currency='EUR Euro' then b.Rate end/case when 

     b.currency='USD US Dollar' then b.Rate end as [EUR/USD] from

    (select er.Rate as Rate, wc.CurrencyCode + ' ' + wc.Descr as [Currency], er.EDate 
    as [Exchange Date], ISNULL(wc.MonSymbol, ' ') AS [Symbol], er2.Rate as [Rate2]
    from ExchangeRate er
    JOIN WorldCurrency wc on er.ExCurrencyId = WC.InCurrencyId
      JOIN ExchangeRate er2 on er.InExchangeRateId = er2.InExchangeRateId
  inner join (
     select wc.CurrencyCode, max(er.EDate) as MaxDate
       from ExchangeRate er
       JOIN WorldCurrency wc on er.ExCurrencyId = WC.InCurrencyId
     group by wc.CurrencyCode
    ) erm on wc.CurrencyCode = erm.CurrencyCode and er.EDate = erm.MaxDate)b

I don't understand why you would like to do this and this is a longshot without knowing the ExchangeRate table. But this should do what you're asking

select er.Rate, 
wc.CurrencyCode + ' ' + wc.Descr as [Currency], 
er.EDate as [Exchange Date], ISNULL(wc.MonSymbol, ' ') AS [Symbol], 
er2.Rate as [Rate2],

erEur.Rate/erUsd.Rate as [EUR/USD]

from ExchangeRate er
JOIN WorldCurrency wc on er.ExCurrencyId = WC.InCurrencyId
JOIN ExchangeRate er2 on er.InExchangeRateId = er2.InExchangeRateId
JOIN ExchangeRate erUsd on er.EDate = erEUR.Edate and erEUR.CurrencyCode = 'USD'
JOIN ExchangeRate erEur on er.EDate = erEUR.Edate and erEUR.CurrencyCode = 'EUR'
inner join (
    select wc.CurrencyCode, max(er.EDate) as MaxDate
    from ExchangeRate er
    JOIN WorldCurrency wc on er.ExCurrencyId = WC.InCurrencyId
    group by wc.CurrencyCode
) erm on wc.CurrencyCode = erm.CurrencyCode and er.EDate = erm.MaxDate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM