I have the following table with monthly data. But we do not have the third month.
| DATE | FREQUENCY |
|---|---|
| 2021-01-01 | 6000 |
| 2021-02-01 | 4533 |
| 2021-04-01 | 7742 |
| 2021-05-01 | 1547 |
| 2021-06-01 | 9857 |
I want to get the frequency of the previous month into the following table.
| DATE | FREQUENCY | PREVIOUS_MONTH_FREQ |
|---|---|---|
| 2021-01-01 | 6000 | NULL |
| 2021-02-01 | 4533 | 6000 |
| 2021-04-01 | 7742 | NULL |
| 2021-05-01 | 1547 | 7742 |
| 2021-06-01 | 9857 | 1547 |
I want the 2021-04-01 record to have NULL for the PREVIOUS_MONTH_FREQ since there is no data for the previous month.
I got so far as...
SELECT DATE,
FREQUENCY,
LAG(FREQUENCY) OVER(ORDER BY DATE) AS PREVIOUS_MONTH_FREQ
FROM Table1