update tb_item_estq set
    vl_estq = vl_estq + tb.vl_qtd_mov
from
(
    select 
        tb_vnd_item.id_emp,
        tb_vnd_item.id_item,
        tb_vnd_item.id_dep,
        tb_vnd_item.id_grd_estq,
        tb_vnd_item.vl_qtd_mov * -1 as vl_qtd_mov
    from tb_vnd_item
        
    union all

    select 
        tb_nf_item.id_emp,
        tb_nf_item.id_item,
        tb_nf_item.id_dep,
        tb_nf_item.id_grd_estq,
        case 
            when tb_nf.tp_op = '[E]' then tb_nf_item.vl_qtd_mov
            when tb_nf.tp_op = '[S]' then tb_nf_item.vl_qtd_mov * -1
        end as vl_qtd_mov
    from tb_nf_item
    join tb_nf on
        (tb_nf_item.id_emp = tb_nf.id_emp and
         tb_nf_item.id_nf = tb_nf.id_nf)
    where
        tb_nf_item.mov_estq 
        
    union all

    select 
        tb_mnt_estq_item.id_emp,
        tb_mnt_estq_item.id_item,
        tb_mnt_estq_item.id_dep_dst as id_dep,
        tb_mnt_estq_item.id_grd_estq,
        case 
            when tb_mnt_estq.op = 'E' then tb_mnt_estq_item.vl_qtd_mov
            when tb_mnt_estq.op = 'S' then tb_mnt_estq_item.vl_qtd_mov * -1
            when tb_mnt_estq.op = 'T' then tb_mnt_estq_item.vl_qtd_mov
        end as vl_qtd_mov
        
    from tb_mnt_estq_item
    join tb_mnt_estq on
        (tb_mnt_estq_item.id_emp = tb_mnt_estq.id_emp and
         tb_mnt_estq_item.id_mnt_estq = tb_mnt_estq.id_mnt_estq)
    
        
    union all

    select 
        tb_mnt_estq_item.id_emp,
        tb_mnt_estq_item.id_item,
        tb_mnt_estq_item.id_dep_org as id_dep,
        tb_mnt_estq_item.id_grd_estq,
        tb_mnt_estq_item.vl_qtd_mov * -1 as vl_qtd_mov
        
    from tb_mnt_estq_item
    join tb_mnt_estq on
        (tb_mnt_estq_item.id_emp = tb_mnt_estq.id_emp and
         tb_mnt_estq_item.id_mnt_estq = tb_mnt_estq.id_mnt_estq)
    where
        tb_mnt_estq.op = 'T'
) as tb
where
    tb_item_estq.id_emp = tb.id_emp and
    tb_item_estq.id_item = tb.id_item and
    tb_item_estq.id_dep = tb .id_dep and
    tb_item_estq.id_grd_estq = tb.id_grd_estq