我必须创建一个报告,显示组织的监督关系层次结构。可以通过多种不同的方式设置监控线路。下面是其中的一些。
Vice President-Associate VP-Director-Assistant Director-Supervisor-worker
Vice President-Associate VP-Director-Supervisor - worker
Vice President-Director-Assistant Director-Supervisor-worker
Vice President-Director-worker
Vice President-Director-Director B-Worker
Vice President-Director-Director B-Supervisor->Worker
Vice President-Associate VP-worker
Vice President-worker
Vice President-Director-Assistant Director
Vice President-Director-worker
这些是层次结构中每个角色的表;所以,一个VP表,AVP表,Director表等等。如果层次结构线不包含某个角色,则该值应返回 null。我编写了程序,所以我从 Worker 表开始,然后从那里加入所有表。我试图考虑所有可能的连接(参见下面的程序)。我使用Supervisor_Position_NBR=POSITION_NBR使用不同的连接组合线条看起来有很多不同的方式,下面是一个小例子:
副总裁 副总裁_副总裁 董事 B 副总裁 _董事 监事 工人 ============== ============ ======== ======= === ===================================简·史密斯乔·罗伯茨马西·詹姆斯·空·空·詹姆斯·马雷·马特·G
·简Smith Joe Roberts Marcy James Null Null James Mare Jess D Jane Smith Joe Roberts Marcy James Null Null Frank jay Carol R Jane Smith Null Marty Bob Null Sonja Null Sam Smith Jane Smith Null Marty Bob Null Sonja Null Nate lowe Jane Smith Null Null Null Null Null拉尔夫·科尔
我的两个问题是:
- 当我有一条包含 VP 、 Director 和 Worker 的监督线,但主管和工作人员都直接向 VP 报告时,当查询运行时,Director 下线,只有工作人员显示。所以这:
副总裁 副总裁 副总裁 董事 B 副董事 主管 工人
=============== =========== ======= ======== == ================= ================= Jane Smith 空 空 空 空 空 空 MarcyJames
当我应该带回这个时:
副总裁 副总裁 副总裁 董事 B 副董事 主管 工人
============= =========== ======== ======== == ======== ======= ========= ===== Jane Smith Null Null Null Null Marcy James
Jane Smith Null Joe Roberts Null Null Null Null
- 有没有更干净更有效的方法来编写这个查询?
非常感谢任何帮助。
select
vp.Vice_President,
avp.Associate_VP,
d.director,
db.director_B,
ad.Assistant_Director,
s.Supervisor,
w.worker
from gw_ppp.dbo.vw_worker w
left join gw_ppp.dbo.vw_Manager_Sup_Role s
on w.Supervisor_Position_NBR=s.POSITION_NBR
left join gw_ppp.dbo.vw_ADIR_Role AD
on w.Supervisor_Position_NBR=AD.POSITION_NBR
or s.Supervisor_Position_NBR=AD.POSITION_NBR
left join gw_ppp.dbo.vw_Dir_Role_B db
on w.Supervisor_Position_NBR=db.POSITION_NBR
or s.Supervisor_Position_NBR=db.POSITION_NBR
or AD.Supervisor_Position_NBR=db.POSITION_NBR
left join gw_ppp.dbo.vw_Dir_Role D
on w.Supervisor_Position_NBR=D.POSITION_NBR
or s.Supervisor_Position_NBR=D.POSITION_NBR
or AD.Supervisor_Position_NBR=D.POSITION_NBR
or db.Supervisor_Position_NBR=D.POSITION_NBR
left join gw_ppp.dbo.vw_AVP_Role AVP
on w.Supervisor_Position_NBR=AVP.POSITION_NBR
or s.Supervisor_Position_NBR=AVP.POSITION_NBR
or AD.Supervisor_Position_NBR=AVP.POSITION_NBR
or D.Supervisor_Position_NBR=AVP.POSITION_NBR
left join gw_ppp.dbo.vw_VP_Role VP
on w.Supervisor_Position_NBR=VP.POSITION_NBR
or s.Supervisor_Position_NBR=vp.POSITION_NBR
or ad.Supervisor_Position_NBR=vp.POSITION_NBR
or d.Supervisor_Position_NBR=vp.POSITION_NBR
or avp.Supervisor_Position_NBR=vp.POSITION_NBR
order by w.worker
这是我用来创建角色视图的程序
CREATE VIEW [vw_VP_Role]
AS
SELECT
File_NBR,
job_title,
(First_Name + ' ' + Last_Name) as Vice_President,
POSITION_NBR ,
Supervisor_Position_NBR,
(Mngr_FName + ' ' + Mngr_LName ) as vp_Manager
FROM [New_EEs].[dbo].[ADPFile]
where Job_title in
('Vice President','Sr VP & Chief Financial Officer','Sr. Vice President');
CREATE VIEW [vw_AVP_Role]
AS
SELECT
File_NBR,
job_title,
(First_Name + ' ' + Last_Name) as Associate_VP,
POSITION_NBR,
Supervisor_Position_NBR,
(Mngr_FName + ' ' + Mngr_LName ) as Avp_Manager
FROM [New_EEs].[dbo].[ADPFile]
where Job_title in
('Associate Vice President','Senior Performance Officer');
CREATE VIEW [vw_Dir_Role]
AS
SELECT
File_NBR,
job_title,
(First_Name + ' ' + Last_Name) as Director,
POSITION_NBR,
Supervisor_Position_NBR,
(Mngr_FName + ' ' + Mngr_LName ) as Dir_Manager
FROM [New_EEs].[dbo].[ADPFile]
where Job_title in
('Director','Chief Information Officer','Deputy Controller','Director of Operations & Staff Dev')
and not (First_Name + ' ' + Last_Name) in ('Michelle James','Edward Lachterman', 'Nafissa Hannat')
CREATE VIEW [vw_Dir_Role_B]
AS
SELECT
File_NBR,
job_title,
(First_Name + ' ' + Last_Name) as Director_B,
POSITION_NBR,
Supervisor_Position_NBR,
(Mngr_FName + ' ' + Mngr_LName ) as Dir_Manager
FROM [New_EEs].[dbo].[vw_ADPFile]
where (First_Name + ' ' + Last_Name) in ('Michelle James','Edward Lachterman', 'Nafissa Hannat')
CREATE VIEW [vw_ADIR_Role]
AS
SELECT
File_NBR,
job_title,
(First_Name + ' ' + Last_Name) as Assistant_Director,
POSITION_NBR,
Supervisor_Position_NBR,
(Mngr_FName + ' ' + Mngr_LName ) as AD_Manager
FROM [New_EEs].[dbo].[ADPFile]
where Job_title= 'Assistant Director'
CREATE VIEW [vw_Manager_Sup_Role]
AS
SELECT
File_NBR,
job_title,
(First_Name + ' ' + Last_Name) as Supervisor,
POSITION_NBR,
Supervisor_Position_NBR,
(Mngr_FName + ' ' + Mngr_LName ) as Sup_Manager
FROM [New_EEs].[dbo].[vw_ADPFile]
where Job_title in ('Supervisor','Campus Administration Manager','Compensation & Benefits Manager',
'Cottage Manager', 'Manager','Office Manager','Operations Manager','Recruiting Manager','Special Projects/Rep & Compliance Manager', 'Talent Manager','Youth Development Coordinator') or (First_Name + ' ' + Last_Name)='Rosa Nunez Pena';
CREATE VIEW [vw_worker]
as
SELECT
File_NBR,
job_title,
(First_Name + ' ' + Last_Name) as worker,
POSITION_NBR,
Supervisor_Position_NBR,
(Mngr_FName + ' ' + Mngr_LName ) as worker_Manager
FROM [New_EEs].[dbo].[ADPFile]
where Job_title not in
('Associate Vice President','Vice President','Sr VP & Chief Financial Officer','Sr. Vice President', 'Director','Chief Information Officer','Deputy Controller','Director of Operations & Staff Dev', 'Assistant Director','Supervisor','Campus Administration Manager','Compensation & Benefits Manager', 'Cottage Manager', 'Manager','Office Manager','Operations Manager','Recruiting Manager','Special Projects/Rep & Compliance Manager', 'Talent Manager','Youth Development Coordinator','Senior Performance Officer') and not (First_Name + ' ' + Last_Name)='Rosa Nunez Pena';