PostgreSQL nested query

0

I have a table used with the following structure:

I want to get the head of each employee who is identified with the head column.

I have the following nested query to get what I'm looking for, but it does not return the names of the boss

select id_empleado, nombre, apellido, 
       (select (empleado.nombre ||' '|| empleado.apellido) as jefe_nombres 
        from empleado where id_empleado = jefe), jefe 
from empleado 

What I want to obtain is that in the header_name column it appears for example for the employee with id 2 jefe_nombres = Pablo Ramiro Vallejo Zuñiga

How could I solve this?

    
asked by Juan Pinzón 06.02.2018 в 19:56
source

1 answer

1

You can make the subquery directly in the select:

select id_empleado,
        nombre,
        apellido,
        jefe,
        (select b.nombre || ' ' || b.apellido from empleado b where b.id_empleado = a.jefe  ) as 'Nombre Jefe'
from empleado a

This is the result (checked):

id_empleado nombre               apellido             jefe        Nombre Jefe                               
----------- -------------------- -------------------- ----------- ----------------------------------------- 
4           Pablo Ramiro         Vallejo Zuñiga       (NULL)      (NULL)                                    
9           Jorge Rafael         Almeida              4           Pablo Ramiro Vallejo Zuñiga               
2           Juan Guillermo       Pinzon               4           Pablo Ramiro Vallejo Zuñiga   
    
answered by 06.02.2018 / 20:12
source