cte_all_itog as (
select
*,
coalesce(svfa_рu, 0)
+ coalesce(ВВА_рu, 0) as s_pu,
coalesce(svfa_рu, 0)
+ coalesce(ВВА_рu, 0)
+ coalesce(ivfa_рu, 0) as itog_pu,
svfa_bu + ВВА_bu as s_bu,
svfa_bu + ВВА_bu + ivfa_bu as itog_bu
from cte_all
)
select
*,
coalesce(svfa_рu, 0)
- svfa_bu as rash_vfa,
coalesce(ВВА_рu, 0)
- ВВА_bu as rash_bba,
s_pu - s_bu as rash_s,
coalesce(ivfa_рu, 0)
- ivfa_bu as rash_i,
itog_pu - itog_bu as rash_itog
from cte_all_itog
order by cid, pid