【发布时间】:2025-12-08 22:30:01
【问题描述】:
我需要根据每种货币的最新交易生成截至月底的汇率。
例如,假设我们需要处理 2 天的汇率(该文件可能会带来 x 天的汇率),它们如下所示:
2015 年 4 月 25 日,英镑,美元,1.8 2015 年 4 月 25 日,美元,英镑,1.25 2015 年 4 月 26 日,英镑,美元,1.7346 2015 年 4 月 26 日,美元,英镑,1.1357在此示例中,我需要在 4/26 获取两条记录并生成到月底的记录,因此输出需要如下所示:
2015 年 4 月 27 日,英镑,美元,1.7346 2015 年 4 月 28 日,英镑,美元,1.7346 2015 年 4 月 29 日,英镑,美元,1.7346 2015 年 4 月 30 日,英镑,美元,1.7346 2015 年 4 月 27 日,美元,英镑,1.1357 2015 年 4 月 28 日,美元,英镑,1.1357 2015 年 4 月 29 日,美元,英镑,1.1357 2015 年 4 月 30 日,美元,英镑,1.1357我所做的是在 PLSQL 中的以下过程,但这似乎并没有像我预期的那样工作。
DECLARE
l_max_date date;
CURSOR C1 IS
SELECT FROM_CURRENCY_DATE,
FROM_CURRENCY,
TO_CURRENCY,
NUMERATOR_BUY,
CONVERSION_RATE,
LEAD(conversion_rate) OVER (PARTITION BY from_currency ORDER BY from_currency_date) AS LEAD_conversion_rate,
ROW_NUMBER() OVER (PARTITION BY from_currency ORDER BY from_currency_date DESC) AS rn
FROM exchange_rate_staging_tbl
WHERE valid_flag is null or valid_flag <> 'E';
cur_rec c1%rowtype;
BEGIN
SELECT MAX(from_currency_date)
INTO l_max_date
FROM exchange_rate_staging_tbl;
FOR cur_rec IN c1 LOOP
if cur_rec.lead_conversion_rate is null then -->Null means it is the latest transaction
dbms_output.put_line(cur_rec.from_currency||' '||
cur_rec.to_currency||' '||
'Inside IF'||' '||
cur_rec.conversion_rate);
ELSE --> Records here are not the latest transaction but they still need to be inserted with their respective exch rate
--dbms_output.put_line(l_max_date||last_day(l_max_date));
dbms_output.put_line(cur_rec.from_currency||' '||
cur_rec.to_currency||' '||
'Inside Else'||' '||
cur_rec.conversion_rate);
END IF;
l_max_date := l_max_date+1;
END LOOP;
END;
上述过程输出:
GBP USD 25-APR-15 25-APR-15 Inside Else 1.8
GBP USD 26-APR-15 26-APR-15 内部 IF 1.7346
我将如何在 SQL 或 PLSQL 中根据每个货币的最新交易生成记录。
【问题讨论】:
-
获取缺失日期列表(例如通过递归查询),选择最大日期和值或这个日期,加入并插入
-
我用我所有的研究和代码编辑了这篇文章。您能详细说明一下您的 cmets 吗?