I have a table for employees where I have running total of sales by every employee in each of the 12 months. Some of the values for sales running total are missing, as below:
e_id | month | sales_run_total |
---|---|---|
e1 | Jan | 10 |
e1 | Feb | null |
e1 | March | null |
e1 | April | 40 |
... | ... | ... |
e1 | Dec | 120 |
e2 | Jan | null |
e2 | Feb | null |
e2 | March | 30 |
e2 | April | 40 |
... | ... | ... |
e2 | Dec | 120 |
I want to impute these missing values with the previous month's sales_run_total value of an employee. If the missing value is for January month, I want to impute it with zero, as below:
e_id | month | sales_run_total | sales_run_total_new |
---|---|---|---|
e1 | Jan | 10 | 10 |
e1 | Feb | null | 10 |
e1 | March | null | 10 |
e1 | April | 40 | 40 |
... | ... | ... | ... |
e1 | Dec | 120 | 120 |
e2 | Jan | null | 0 |
e2 | Feb | null | 0 |
e2 | March | 30 | 30 |
e2 | April | 40 | 40 |
... | ... | ... | ... |
e2 | Dec | 120 | 120 |
I have tried to do it using lag() function, as below :
select e_id,
month,
lag(sales_run_total) over (partition by e_id, month) as sales_run_total_new
from table_name
But this does not consider whether there is a null or non-null value in actual column. Also, it does not consider the case where we have null for January month.
How can this be done in SQL?