In the above attached picture, I want to perform the operation of (run_tot - lag_data)/run_tot. But how to perform that with null values in lag_data. My code is,
select *, ((running_total-lag_data)/running_total) as per_incr_bal
from
(select *, lag(running_total) over(partition by customer_id
order by m) as lag_data
from
(select *, SUM(total) OVER (
partition by customer_id
order by m
rows unbounded preceding) as running_total
from(
select customer_id, extract (month from txn_date) as m,
sum(case when txn_type = 'deposit' then txn_amount
else -txn_amount
end) as total
from data_bank.customer_transactions
group by customer_id, m
order by customer_id, m) t1) t2)t3
where lag_data is not null
I am getting a div by zero error.
