update tb_item_estq set
    vl_estq = tb.vl_estq
from
(
    select 
        tb.id_emp,
        tb.id_dep,
        tb.id_grd_estq,
        tb.id_item,
        tb_item.descr,
        sum(vl_qtd_mov) as vl_estq
    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
        join tb_vnd on
            (tb_vnd_item.id_emp = tb_vnd.id_emp and
             tb_vnd_item.id_vnd = tb_vnd.id_vnd)
        where
            tb_vnd_item.mov_estq and
            coalesce(tb_vnd.canc, false) = false and
            tb_vnd.id_vnd > 0
            
        union all

        select 
            tb_nf_item.id_emp,
            tb_nf_item.id_item,
            tb_nf_item.id_dep,
            tb_nf_item.id_grd_estq,
            tb_nf_item.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 and
            tb_nf.id_nf > 0 and
            tb_nf_item.mov_estq and
            tb_nf.tp_op = '[E]'
            
            
        union all

        select 
            tb_nf_item.id_emp,
            tb_nf_item.id_item,
            tb_nf_item.id_dep,
            tb_nf_item.id_grd_estq,
            tb_nf_item.vl_qtd_mov * -1 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 and
            tb_nf.id_nf > 0 and
            tb_nf_item.mov_estq and
            tb_nf.tp_op = '[S]'


        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)
        where
            tb_mnt_estq_item.id_mnt_estq > 0
            
        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' and
            tb_mnt_estq_item.id_mnt_estq > 0
            
        union all 
        
        select 
            tb_pdc_colab_item.id_emp,
            tb_pdc_colab_item.id_item,
            tb_pdc_colab_item.id_dep as id_dep,
            tb_pdc_colab_item.id_grd_estq,
            tb_pdc_colab_item.vl_qtd as vl_qtd_mov
            
        from tb_pdc_colab_item
        join tb_pdc_colab on
            (tb_pdc_colab_item.id_emp = tb_pdc_colab.id_emp and
             tb_pdc_colab_item.id_pdc_colab = tb_pdc_colab.id_pdc_colab)
        
    ) as tb
    join tb_item on 
        (tb.id_item = tb_item.id_item)
    group by
        tb.id_emp,
        tb.id_dep,
        tb.id_grd_estq,
        tb.id_item,
        tb_item.descr
    order by 
        id_item
) as tb
where
    tb_item_estq.id_item = tb.id_item and
    tb_item_estq.id_emp = tb.id_emp and
    tb_item_estq.id_dep = tb.id_dep and
    tb_item_estq.id_grd_estq = tb.id_grd_estq