Skip to content
Advertisement

sql – how to join on a column that is less than another join key

I have two tables as below. What I’m trying to do is to join A and B base on date and id, to get the value from B. The problem is, I want to join using add_month(A.Date, -1) = B.month (find the data in table B from one month earlier). If that’s not available, I want to join using two months earlier add_month(A.Date, -2) = B.month How can I achieve this in one query? In the result all 3 rows should be joint. Spark sql is preferred instead of api. Many thanks.

Advertisement

Answer

Once way I can think of is , you can create the required lag date for A columns and join with date with B as below –

Data Preparation

Join – Spark API

Join – SparkSQL

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