select
    g.id,
    g.descricao,
    g.remessas as remessas_permitidas,

    coalesce(( select
            count(remessa_id)
        from(
            select distinct
                s.remessa_id,
                coalesce((select first 1 1 from tbprogramacao c where c.remessa_id = s.remessa_id and c.status > 1 and c.status < 6),0) as INICIADOS
            from tbprogramacao s
            where s.gp_id = g.id
            and s.tipo = 'A'
            and s.status < 2
            and s.requisicao_id = 0

            order by s.remessa_id
        ) where INICIADOS > 0
    ),0) as em_aberto

from tbgp g
where g.familia_id = 3