select distinct a.* from pbx_funcoes a, pbx_funcoes b where b.fun_id_pai = a.fun_id and coalesce(a.fun_id_pai, 0) = 0 and a.fun_public = 0 select distinct b.* from pbx_funcoes a, pbx_funcoes b where b.fun_id_pai = a.fun_id and b.fun_public = 0