How do I use ifnull with a window function?
Let's say I have this query result below, lag1 is just the budget column shifted down using the window function, because of that, the value is null. But I want to replace that null with a zero so I can calculate the difference between budget and lag1.
select id, budget,
lag(budget) over (order by id) as lag1
from projects;
+----+---------+---------+
| id | budget | lag1 |
+----+---------+---------+
| 1 | 1000000 | NULL |
| 2 | 100000 | 1000000 |
| 3 | 100 | 100000 |
+----+---------+---------+
I tried the following two examples but it doesn't work:
select id, budget,
ifnull(lag(budget),0) over (order by id) as lag1
from projects;
select id, budget,
ifnull((lag(budget) over (order by id) as lag1),0)
from projects;