You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于月份匹配汇率并补全缺失期汇率的SQL查询问题

需求与问题说明

我有两张表:sales和currency_rate,需要根据sales表的Date列(整数格式,如20240701)和currency_rate表的CurrentPeriod列(datetime类型),为sales的每笔交易填充CurrRate列。如果currency_rate表的日期序列存在中断,就用上月的CurrRate值填充对应sales记录。

示例表

sales表

DateValue
202304041226.55
202305068.00
2023071713834.73
2023091610120.71
202309048.00
20230506102.14
202309128.00
2023052612813.40
20230805779.90

currency_rate表(模拟某一期为NULL)

CurrentPeriodPreviousPeriodCurrRate
2023-04-01T00:00:00.0000000(NULL)0.894166
2023-05-01T00:00:00.00000002023-04-01T00:00:00.00000000.893192
2023-06-01T00:00:00.00000002023-05-01T00:00:00.00000000.879856
2023-07-01T00:00:00.00000002023-06-01T00:00:00.00000000.887729
(NULL)2023-07-01T00:00:00.00000000.896162
2023-09-01T00:00:00.00000002023-08-01T00:00:00.00000000.898093

预期结果

DateValueCurrRate
202304041226.550.894166
202305068.000.893192
2023071713834.730.887729
2023091610120.710.898093
202309048.000.898093
20230506102.140.893192
202309128.000.898093
2023052612813.400.893192
20230805779.900.887729

可以看到,20230805对应的CurrRate是2023年7月的汇率0.887729,因为2023年8月的汇率记录缺失。

当前疑问与问题

  1. 我用LAG函数生成currency_rate表的PreviousPeriod列,但不确定ORDER BY应该用CurrentPeriod ASC还是DESC?
  2. 如何编写Spark SQL(或T-SQL,我会自行转换为Spark SQL)语句实现正确关联,同时处理日期为NULL的情况?我已经用以下语句处理数据类型转换用于关联:
date_format(rates.currentperiod, 'yyyyMM') = date_format(to_date(cast(sales.date as string), 'yyyyMMdd'), 'yyyyMM')

我的现有查询语句如下:

SELECT
    inv.*,
    IFNULL(cr.CurrRate, 0) AS CurrencyRate
FROM
    v_sales inv
LEFT JOIN (
    SELECT
        CurrentPeriod,
        LAG(CurrentPeriod) OVER (PARTITION BY curr_rate_from, curr_rate_to ORDER BY CurrentRate) AS PreviousPeriod,
        curr_rate_from,
        curr_rate_to,
        CurrRate
    FROM
        schema.currency_rate
) cr ON
    inv.curr_code = cr.curr_rate_from -- 这部分不重要
    AND (
        (date_format(to_date(cast(inv.Day AS STRING), 'yyyyMMdd'), 'yyyyMM') = date_format(cr.CurrentPeriod, 'yyyyMM'))
        OR
        (date_format(to_date(cast(inv.Day AS STRING), 'yyyyMMdd'), 'yyyyMM') < date_format(cr.CurrentPeriod, 'yyyyMM') 
        AND date_format(to_date(cast(inv.Day AS STRING), 'yyyyMMdd'), 'yyyyMM') >= date_format(cr.PreviousPeriod, 'yyyyMM'))
    )

但查询结果中CurrencyRate列仍存在一些不应出现的0值,需要解决这个问题。


内容的提问来源于stack exchange,提问作者Data_engineer_noobie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 11:14:54