Big Query Join on Closest Date

Viewed 407

Hello I'm trying to join two tables in bigquery using two fields, one of them is a date field and the other a currency_code.

Example data:

sales_table
|sales_id|currency|   date   |value| 
|1234    |GBP     |2020-01-10| 1.50|
|1235    |GBP     |2020-01-15| 1.48|
|1236    |GBP     |2020-01-20| 1.49|
currency_table
|currency|   date   | rate|
|GBP     |2020-01-10| 0.89|
|GBP     |2020-01-15| 0.89|
|GBP     |2020-01-19| 0.89|

The idea is to get the exchange_rate for a particular date I don't always have all the dates on currency_table so I would like to join on nearest value. How can I achieve this? I tried using CROSS JOIN but the size of the data (5M Rows on sales table) makes it almost impossible to work with. Any ideas?

1 Answers

You can do this using window functions:

with cte as (
      select sales_id, currency, date, value, null as rate
      from sales 
      union all
      select null, currency, date, null, rate
      from currency_table
     )
select sales_id, currency, date, value, imputed_rate
from (select cte.*,
             last_value(rate ignore nulls) over (partition by currency order by date) as imputed_rate
      from t
     ) t
where sales_id is not null;
Related