TSQL - 如何根据同一用户的另一种货币汇率确定用户货币汇率是否正确

TSQL - How to find out if a Users Currency rate is correct based on another currency rate for the same user

我们目前遇到一个问题,用户的某些费率输入不正确,我的任务是查明哪些条目不正确,但不幸的是我有点卡住了。

要求如下;

以下是我开始的方式:

我们有一个名为 TIMERATE 的 table,其中输入用户收费率,每次费率变化时都会添加一个新行。用户有可能使用多种货币的收费率。

这里是 table 中针对单个用户的数据截取;

tkinit  tkeffdate   tkrt01  tkrtcur
LAU 01/02/2014  170 GBP
LAU 01/08/2014  260 GBP
LAU 01/12/2014  130 GBP
LAU 01/08/2014  260 USD
LAU 01/12/2014  210 USD
LAU 01/02/2015  260 USD

要找出每种货币的最新汇率:

SELECT
    TKINIT as Timekeeper,
    MAX (tkeffdate) as MaxEffectiveDate,
    tkrtcur as Currency,
    Cast (NULL as decimal (16,2)) as Rate
INTO
    #LatestRate
FROM
    TIMERATE
GROUP BY
    TKINIT, tkrtcur
ORDER BY
    TKINIT

然后我更新了温度中的速率 table

update 
    #LatestRate
Set 
    Rate = tkrt01
from 
    #LatestRate
JOIN 
    Timerate on TKINIT = Timekeeper 
        and tkrtcur = Currency 
        and tkeffdate = MaxEffectiveDate

所以我现在有每个用户的每种货币的最新汇率,但我不知道如何处理数据以满足要求

有人有什么想法吗?我是不是走错了路?

在 tkinit 和日期上将 table 加入自身,其中一侧是 gbp,另一侧是 usd。这将使你们都在同一行,因此您可以检查汇率是否正确。

Rabbit 的回答是比较转换是否正确的最简单方法。 从最初的步骤开始,您可以 运行 在 #LatestRate table 上进行此查询以获得结果。请确保 case 语句中的比较工作正常。我没有测试过这段代码。

SELECT
    GBP.Timekeeper as Timekeeper,
    GBP.MaxEffectiveDate as EffectiveDate,
    GBP.Currency as GBP,
    GBP.Rate as GBPRate,
    USD.Currency as USD,
    USD.Rate as USDRate,
    case 
        when USD.Rate is null then 'GBP Only' 
        when round(USD.Rate/1.55,2)=round(GBP.Rate,2) then 'Correct Conversion'
        else 'FALSE'
    end as IsConversion
FROM #LatestRate GBP
INNER JOIN #LatestRate USD
ON GBP.Timekeeper=USD.Timekeeper
and GBP.MaxEffectiveDate=USD.MaxEffectiveDate
and GBP.Currency='GBP' and USD.Currency='USD'