Hello,
I would like to generate a table like this:
Level PersonName empid ManagerID Code ManagerName
1 Big Boss 1 null 00 null
2 Executive 1 2 1 0001 Big Boss
2 Executive 2 3 1 0002 Big Boss
3 Manager 1 4 2 000101 Executive 1
3 Manager 2 5 2 000102 Executive 1
3 Manager 3 6 3 000201 Executive 2
So, as you can see a subordinate has 2 more digits in his code as compared to his manager and the code determines the hierarchy. Sometimes during reorganization, employee's codes are not updated immediately, so the coding becomes inconsistent generating an error in the table like so:
Level PersonName id ManagerID Code ManagerName
1 Big Boss 1 null 00 null
2 Executive 1 2 1 0001 Big Boss
2 Executive 2 3 1 0002 Big Boss
3 Manager 1 4 2 000101 Executive 1
3 Manager 2 5 2 000102 Executive 1
3 Manager 3 6 3 000201 Executive 2
3 Manager 4 7 99 000202 Wrong Manager
So, I would like to add a condition that checks that if a person's manager id exists as the id in the level above. For instance in level 3, Manager 4's manager id is 99. The manager id, 99 does not exist as a person's id in level 2, therefore it should not be included.
This is my attempt at the code:
select 1 as level ,reportfor.preferredname + ' ' + reportfor.surname as PersonName ,id ,null as ManagerID ,emp.code as Code ,null as ManagerName from d_edr edr inner join o_training_courses crs on crs.sap_course_num=edr.course_id inner join employees emp on emp.empid=edr.empid inner join employees reportfor on reportfor.code = '00' group by ,reportfor.preferredname + ' ' + reportfor.surname ,id ,emp.code union select 2 as level ,reportfor.preferredname + ' ' + reportfor.surname as PersonName ,id ,emp.mgrid as ManagerID ,emp.code as Code ,mgr.preferredname + ' ' + mgr.surname as ManagerName from d_edr edr inner join o_training_courses crs on crs.sap_course_num=edr.course_id inner join employees emp on emp.empid=edr.empid inner join employees reportfor on reportfor.code LIKE '00__' inner join employees mgr on mgr.empid=reportfor.mgrid group by ,reportfor.preferredname + ' ' + reportfor.surname ,id ,emp.mgrid ,emp.code ,mgr.preferredname + ' ' + mgr.surname union select 3 as level ,reportfor.preferredname + ' ' + reportfor.surname as PersonName ,id ,emp.mgrid as ManagerID ,emp.code as Code ,mgr.preferredname + ' ' + mgr.surname as ManagerName from d_edr edr inner join o_training_courses crs on crs.sap_course_num=edr.course_id inner join employees emp on emp.empid=edr.empid inner join employees reportfor on reportfor.code LIKE '00____' inner join employees mgr on mgr.empid=reportfor.mgrid group by ,reportfor.preferredname + ' ' + reportfor.surname ,id ,emp.mgrid ,emp.code ,mgr.preferredname + ' ' + mgr.surname
I was hoping that this line of code would take care of it, but it doesn't
inner join employees mgr on mgr.empid=reportfor.mgrid
THe definition of the employee table is:
empid = employee id
preferred name = first name
surname = last name
code = hierarchy code
mgrid = manager id
Any help is appreciated. Thanks in advance!