Skip to content
Advertisement

SQL Join using a Junction Table not returning expected results

I’m joining 3 tables in SQL Server for the purpose of mapping users to their managers. The purpose of the junction table is to map their full usernames to their network ID. Here’s the query:

The first 3 columns appear as I’d expect, showing the Network ID, Full User Name and the Manager’s Full Name. However, the 4th column is the issue. It’s showing the network ID of the user, not the manager.

It appears like this:

It should appear like this:

The Employee.Records table contains all full usernames and full supervisor names. The Employee.Records_MANAGER_S_ table is used to tie the Supervisors to the users, since each user could have multiple supervisors. All of the mappings for Network Names and Full Names are also in the Employee.Records table, so technically I’m trying to join the Employee.Records table to the Employee_Records_MANAGER_S_ and then do another join of the Employee_Recors_MANAGER_S_ back to the Employee.Records table again, but this time joining on the SUPERVISOR_FULLNAME instead of the Employee’s name.

Advertisement

Answer

If I understand correctly you need to join your dbo.Employee_Records table on a second time to get the supervisor details.

Note: As shown I highly recommend short table aliases as they make the query much clearer.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement