update cupons c INNER JOIN (select m.nfce AS 'nfce', sum(m.desconto)+sum(m.desconto_item) as 'desconto' from movdiario m where m.emissao between '20200801' and '20200909' and m.SITUACAO is null and m.nfce <> 0 group by m.nfce) t ON c.NFCE = t.nfce SET c.VL_DESCONTO = t.desconto where c.cstat not in (100,102,135,150) and c.emissao between '20200801' and '20200909' and c.tipo = 'RV - CUPOM FISCAL' and c.nfce <> 0