Hi I have a a table as :
| date_key | month | customer_id |
|---|---|---|
| 2022-01-01 | 1 | 1 |
| 2022-01-23 | 1 | 1 |
| 2022-02-02 | 2 | 1 |
| 2022-02-15 | 2 | 1 |
| 2022-02-16 | 2 | 1 |
| 2022-02-18 | 2 | 1 |
| 2022-02-16 | 2 | 1 |
| 2022-05-18 | 5 | 1 |
| 2022-06-11 | 6 | 1 |
| 2022-06-12 | 6 | 1 |
| 2022-06-13 | 6 | 1 |
| 2022-06-15 | 6 | 1 |
and want to lag on last previous month above
| date_key | month | customer_id | lastMonth |
|---|---|---|---|
| 2022-01-01 | 1 | 1 | |
| 2022-01-23 | 1 | 1 | |
| 2022-02-02 | 2 | 1 | 1 |
| 2022-02-15 | 2 | 1 | 1 |
| 2022-02-16 | 2 | 1 | 1 |
| 2022-02-18 | 2 | 1 | 1 |
| 2022-02-16 | 2 | 1 | 1 |
| 2022-05-18 | 5 | 1 | 2 |
| 2022-06-11 | 6 | 1 | 5 |
| 2022-06-12 | 6 | 1 | 5 |
| 2022-06-13 | 6 | 1 | 5 |
| 2022-06-15 | 6 | 1 | 5 |
I tried using
select *
lag(month,1) over(partition by customer_id order by month) lastMonth
from table
However this does not seem to get the result as needed. Please do help.