Скрипт суммирует суммы выплат всех месяцев во всех пачках года до указанного квартала включительно.
Нужно изменить номера кварталов - которые суммируем и в который кладем, а так же год.
set summa_1 = q0.summa_1
from z_pachka as zp
join z_rep_pens as pens on pens.id_pachka = zp.id
join pftb_payout as pftb on pftb.id_pens = pens.id
join (
select
sum(pftb.summa_1) as summa_1,
pens.auto_card as auto_card,
pftb.pens_categ as pens_categ
from z_pachka as zp
join z_rep_pens as pens on pens.id_pachka = zp.id
join pftb_payout as pftb on pftb.id_pens = pens.id
join pr_pfr_cat as cat on cat.sort_num = pftb.pens_categ
where zp.t_pach = 1
and zp.a_year = 2015
and zp.id_period in (101,102,103) -- суммируемые кварталы
and pftb.p_month <> 0
--and pens.auto_card = 115063
group by pens.auto_card, pftb.pens_categ
) as q0 on q0.auto_card = pens.auto_card
and q0.pens_categ = pftb.pens_categ
join pftb_payout on pftb_payout.id_payout = pftb.id_payout
where zp.t_pach = 1
and zp.a_year = 2015
and zp.id_period in (103) --в какой квартал пробиваем
and pftb.p_month = 0 --всего
-------------------------------summa_2
update pftb_payout
set summa_2 = q0.summa_2
from z_pachka as zp
join z_rep_pens as pens on pens.id_pachka = zp.id
join pftb_payout as pftb on pftb.id_pens = pens.id
join (
select
sum(pftb.summa_2) as summa_2,
pens.auto_card as auto_card,
pftb.pens_categ as pens_categ
from z_pachka as zp
join z_rep_pens as pens on pens.id_pachka = zp.id
join pftb_payout as pftb on pftb.id_pens = pens.id
join pr_pfr_cat as cat on cat.sort_num = pftb.pens_categ
where zp.t_pach = 1
and zp.a_year = 2015
and zp.id_period in (101,102,103) -- суммируемые кварталы
and pftb.p_month <> 0
--and pens.auto_card = 115063
group by pens.auto_card, pftb.pens_categ
) as q0 on q0.auto_card = pens.auto_card
and q0.pens_categ = pftb.pens_categ
join pftb_payout on pftb_payout.id_payout = pftb.id_payout
where zp.t_pach = 1
and zp.a_year = 2015
and zp.id_period in (103) --в какой квартал пробиваем
and pftb.p_month = 0 --всего
--------------summa_3
update pftb_payout
set summa_3 = q0.summa_3
from z_pachka as zp
join z_rep_pens as pens on pens.id_pachka = zp.id
join pftb_payout as pftb on pftb.id_pens = pens.id
join (
select
sum(pftb.summa_3) as summa_3,
pens.auto_card as auto_card,
pftb.pens_categ as pens_categ
from z_pachka as zp
join z_rep_pens as pens on pens.id_pachka = zp.id
join pftb_payout as pftb on pftb.id_pens = pens.id
join pr_pfr_cat as cat on cat.sort_num = pftb.pens_categ
where zp.t_pach = 1
and zp.a_year = 2015
and zp.id_period in (101,102,103) -- суммируемые кварталы
and pftb.p_month <> 0
--and pens.auto_card = 115063
group by pens.auto_card, pftb.pens_categ
) as q0 on q0.auto_card = pens.auto_card
and q0.pens_categ = pftb.pens_categ
join pftb_payout on pftb_payout.id_payout = pftb.id_payout
where zp.t_pach = 1
and zp.a_year = 2015
and zp.id_period in (103) --в какой квартал пробиваем
and pftb.p_month = 0 --всего
--------------summa_2_1
update pftb_payout
set summa_2_1 = q0.summa_2_1
from z_pachka as zp
join z_rep_pens as pens on pens.id_pachka = zp.id
join pftb_payout as pftb on pftb.id_pens = pens.id
join (
select
sum(pftb.summa_2_1) as summa_2_1,
pens.auto_card as auto_card,
pftb.pens_categ as pens_categ
from z_pachka as zp
join z_rep_pens as pens on pens.id_pachka = zp.id
join pftb_payout as pftb on pftb.id_pens = pens.id
join pr_pfr_cat as cat on cat.sort_num = pftb.pens_categ
where zp.t_pach = 1
and zp.a_year = 2015
and zp.id_period in (101,102,103) -- суммируемые кварталы
and pftb.p_month <> 0
--and pens.auto_card = 115063
group by pens.auto_card, pftb.pens_categ
) as q0 on q0.auto_card = pens.auto_card
and q0.pens_categ = pftb.pens_categ
join pftb_payout on pftb_payout.id_payout = pftb.id_payout
where zp.t_pach = 1
and zp.a_year = 2015
and zp.id_period in (103) --в какой квартал пробиваем
and pftb.p_month = 0 --всего
|