Код: Выделить всё
select sum(p.sum_pr*p.proc/100) from
(
select u.FK_BD_TOVAR,
u.DATE_SET,
coalesce(u.DATE_END,cast(:date_end as date)) as date_end,
cast(u.val as decimal(10,3)) proc,
sum(bd_11.cena_pr_with_nds*bd_11.cnt) as cnt,
sum(bd_11.cena_pr_with_nds*bd_11.cnt) sum_pr
from bd_1 join bd_11 on bd_11.fk_bd_1=bd_1.field_key
join BD_TOVAR_USERPROPS u on u.FK_BD_TOVAR=bd_11.FK_BD_TOVAR and bd_1.DATE_PRIH between u.DATE_SET and coalesce(u.DATE_END,cast(:date_end as date))
join BD_USERPROPS on BD_USERPROPS.FIELD_KEY=u.FK_BD_USERPROPS and BD_USERPROPS.TABLENAME='BD_TOVAR' and BD_USERPROPS.NAME_VAR='spis'
where bd_1.date_prih between cast(:date_start as date) and cast(:date_end as date) and bd_1.fk_bd_firm=:fk_bd_firm and bd_1.valid_flag=1
group by 1,2,3,4
) p