SQL Server, query data from a table using joins

0

Good, I need help because I can not find a problem, I have a table 'A' of meetings and a table 'B' of users, table A contains a field called 'Organizer', which stores the Login of a user (eg: Juan Perez, your login it's 'jperez'). Table B stores the users and information of each one (Full name, Email, Area, Boss ID) when building my query, I can get the full name of the Organizer user by making a Join to the Users table and adding the field directly:

Select A.[ID],A.[Asunto],A.[IDObjetivo],A.[Ubicacion],A.[Organizador],A.[FechaCreacion],A.[FechaReunion],u.[fullname] AS Ejecutivo from [AgendaReuniones] A  
JOIN users u ON
  A.[Organizador] = u.[username] 

My big question is the following, how can I get the Chief of the Organizer ?, in the Users table there is a field called 'IDJefe' as shown below:

ID     UserLogin        IDJefe
1001   Jefe1            NULL
1002   Jefe2            NULL
1003   Usuario1         1001
1004   Usuario2         1001
1004   Usuario3         1002

Table Meetings (In Red the field Related to 'Users')

Users Table (In Red the field related to 'Meetings')

    
asked by Ariel Ignacio 16.10.2017 в 22:04
source

2 answers

1

You can use the join with the second table again with an alias as if it were a third one like this:

Select A.[ID],A.[Asunto],A.[IDObjetivo],A.[Ubicacion],A.[Organizador],A.[FechaCreacion],A.[FechaReunion],u.[fullname] AS Ejecutivo, b.[fullname] AS Jefe
from (([AgendaReuniones] A  
LEFT JOIN users u ON A.[Organizador] = u.[username]) 
LEFT JOIN users b on u.IDJefe = b.ID)
    
answered by 16.10.2017 / 22:33
source
0

they are only 2 tables, I imagine that it should not be very complex, Is there a Primary key that will be split from tb1 to tb2 or vice versa? with an INNER JOIN you could do it in that case. and if you want to bring the bosses that are NULL, I imagine that you could condition the field with an IS NULL.

    
answered by 16.10.2017 в 22:15