SELECT m.entidad_id, COALESCE(e.nombre, 'No existe nombre') AS nombre_sigemweb, m.dpto_snc, m.dist_snc, COALESCE(r.nom_dist, 'No existe nom_dist') AS nom_dist_snc FROM public.snc_catalog_mapping m LEFT JOIN public.snc_raw_distritos r ON m.dpto_snc = r.cod_dpto AND m.dist_snc = r.cod_dist LEFT JOIN LATERAL ( SELECT nombre FROM dblink('host=192.168.1.254 user=postgres password=x25yvaga2017 dbname=sigemweb', 'SELECT nombre FROM public.entidades WHERE entidad = ' || m.entidad_id::text) AS t(nombre text) ) e ON true WHERE m.dpto_snc = 'H' ORDER BY m.entidad_id;