I have a table say customer
DATE | ID | TYPE |
---|---|---|
2018-01-01 | 1 | FI |
2019-01-01 | 1 | LF |
2020-01-01 | 1 | LF |
2021-01-01 | 1 | FI |
2022-01-01 | 1 | LF |
Now what I want to do is add a new column ' NEW_DATE ' in which I have a logic as follows:
if type = FI then set it NULL
Else if LF then take the lowest Date which is following the continuous order
Expected output:
DATE | ID | TYPE | NEW_DATE |
---|---|---|---|
2018-01-01 | 1 | FI | NULL |
2019-01-01 | 1 | LF | 2019-01-01 |
2020-01-01 | 1 | LF | 2019-01-01 |
2021-01-01 | 1 | FI | NULL |
2022-01-01 | 1 | LF | 2022-01-01 |
In this the2nd and 3rd row have continuous LF code so it will take the lowest date i.e, 2019-01-01 and in the 4rth FI is there so null and chain breaks. So 5th Row gets 2022-01-01
Now I want to use window function like
CASE
WHEN TYPE <> 'LF'
THEN NULL
ELSE MIN(DATE) OVER (PARTITION BY TYPE ORDER BY DATE)
END AS NEW_DATE
but this considers all types as one whole. So what can be the solution to this?