Skip to content
Advertisement

SQL – Join with window

I have this dataset of product sales:

And I have this dataset of Financial Sales

I want to create a column in the dataset PRODUCT SALES named “If_financial_sales” where it takes the value 1 when the client in product sales bougth a financial product in the last 24 months, and 0 otherwise. Those 24 months are counting from the YearMonth of PRODUCT SALES (not from today). For example, If the client 2 on 201802 from product sales date bought a financial sale in the last 24 months (from 201801 to 201601 = 24 months) then If_financial_sales = 1 for this client in that month.

Output expected:

I have been trying this with ROW_NUMBER(), but it seems to work better with some joins I have seen that uses equality operatos on the keys when joining (>, <, =) but I don’t know what their name is neither how to use them.

Advertisement

Answer

Try this:

Output:

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