Skip to content
Advertisement

BigQuery, NULL values when joining transactions and currency exchange tables

I need to convert transactions imports over the last 3 years present in a table to €. To do this, I took a separate table with conversion values. So I need to join this two tables, attach the right conversion rate to the first table. The rates table has two fields (valid_from and valid_to) that contain the rates for the last 3 years. So I must attach the rate referring to the right currency (for €, we have a single rate = 1) at the right moment in time. Here the query I built:

I created valid_to manually simply putting the valid_from values of one period to the valid_to of the previous one. When the rate is still valid now I put a 3001-12-31.

The problem with this query is that it returns NULL values for all the rates fields. I checked the two separate tables and they are fine. The funny thing is that if I filter the rates table for only one currency it works fine (no NULL values for transactions made with that currency), otherwise not. If I try to filter on two or more currencies, it returns NULL values for all transactions as well.

Advertisement

Answer

You need the currency in the lead():

Note that I also simplified the valid_to logic using the three argument form of lead().

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement