I'm using pgAdmin4 and I have a SQL table with employee/manager HR data that looks like this:
| employee_id | email_address | full_name | band_lvl | manager_id |
| 5592 | jillr@ex.org | Jill Rhode | 20 | 6521 |
| 6421 | racheln@ex.org | Rachel Nam | 40 | 4251 |
| 2818 | todda@ex.org | Todd Alex | 25 | 6421 |
| 4251 | jalens@ex.org | Jalen Smith | 60 | 2199 |
| 6521 | tolun@ex.org | Tolu Nagoye | 30 | 2199 |
| 7831 | jina@ex.org | Ji Na | 80 | NULL |
| 2199 | zaynm@ex.org | Zayn Mate | 70 | 7831 |
Based on the first manager_id and employee_id, I'm seeking to return the following columns: Level1 Manager Name, Level1 Manager Email, Level1 Manager Band Lvl, Level1 Manager Manager's Id. I then want to do that for each manager that's a step above, until there are no higher managers.
The desired output should look like this:
| employee_id | email_address | full_name | band_lvl | manager_id | Lvl1 Mng Nm | Lvl1 Mng Email | Lvl1 Mng Band Lvl | Lvl1 Mng Mngs Id | Lvl2 Mng Nm | Lvl2 Mng Email | Lvl2 Mng Band Lvl | Lvl2 Mng Mngs Id |
| 5592 | jillr@ex.org | Jill Rhode | 20 | 6521 | Tolu Nagoye | tolun@ex.org | 30 | 2199 | Zayn Mate | zaynm@ex.org | 70 | 7831 |
| 6421 | racheln@ex.org | Rachel Nam | 40 | 4251 | Jalen Smith | jalens@ex.org | 60 | 2199 | Zayn Mate | zaynm@ex.org | 70 | 7831 |
| 2818 | todda@ex.org | Todd Alex | 25 | 6421 | Rachel Nam | racheln@ex.org | 40 | 4251 | Jalen Smith | jalens@ex.org | 60 | 2199 |
| 4251 | jalens@ex.org | Jalen Smith | 60 | 2199 | Zayn Mate | zaynm@ex.org | 70 | 7831 | Ji Na | jina@ex.org | 80 | NULL |
| 6521 | tolun@ex.org | Tolu Nagoye | 30 | 2199 | Zayn Mate | zaynm@ex.org | 70 | 7831 | Ji Na | jina@ex.org | 80 | NULL |
| 7831 | jina@ex.org | Ji Na | 80 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 2199 | zaynm@ex.org | Zayn Mate | 70 | 7831 | Ji Na | jina@ex.org | 80 | NULL | NULL | NULL | NULL | NULL |
So far, this is what I've come up with, to get the first columns for the Level 1 Manager; however, I don't know where to go from here, as I'm very new to SQL:
SELECT B.employee_id,
B.email_address,
B.full_name,
B.band_lvl,
B.manager_id,
B1.full_name AS L1_mng_nm,
B1.email_address AS L1_mng_email,
B1.band_lvl AS L1_mng_band_lvl,
B1.manager_id AS L1_mgr_mgrs_id
FROM hrdata B
INNER JOIN hrdata B1 ON
B.manager_id = B1.employee_id;