我正在尝试使用以下两个查询创建一个视图,并收到错误消息视图或函数在临时表上是不允许的。有没有办法做到这一点,或者有没有办法结合下面的两个查询,所以我不必使用临时表?
create view
[vw_Org_Hierarchy]
as
**--Query 1**
SELECT
File_NBR,
Job_title,
First_Name + ' ' + Last_Name AS Name,
CASE WHEN job_title IN ('Vice President', 'Sr VP & Chief Financial Officer',
'Sr. Vice President', 'Executive Manager') THEN 'Vice President'
WHEN job_title IN ('Associate Vice President', 'Associate Vice President & CPO',
'Associate Vice President & CIO') THEN 'Associate VP'
WHEN job_title IN ('Acting Medical Director', 'Director', 'Deputy Controller', 'Director of Operations & Staff Dev') AND NOT (First_Name + ' ' + Last_Name) IN ('Michelle James', 'Edward Lachterman', 'Nafissa Hannat')
THEN 'Director'
WHEN (First_Name + ' ' + Last_Name) IN ('Michelle James', 'Edward Lachterman', 'Nafissa Hannat') THEN 'Director B'
WHEN Job_title = 'Assistant Director' THEN 'Assistant Director'
WHEN job_title IN ('Coordinator', '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') THEN 'Supervisor' ELSE 'Worker' END AS Job_Category, Position_NBR, Supervisor_Position_NBR, Mngr_FName + ' ' + Mngr_LName AS Manager
INTO **#STAFF**
FROM New_EEs.dbo.vw_ADPFile
**--Query 2**
WITH org AS
(SELECT
s.POSITION_NBR,
s.File_NBR,
s.Name,
s.Job_Category
FROM **#STAFF** AS s
UNION ALL
SELECT
s.POSITION_NBR,
s.File_NBR,
o.Name,
o.Job_Category
FROM **#STAFF** AS s
JOIN org o ON (o.POSITION_NBR=s.Supervisor_Position_NBR))
SELECT
[Vice President] as 'Vice President',
[Associate VP] AS 'Associate VP',
[Director] AS 'Director',
[Director B] AS 'Director B',
[Assistant Director] AS 'Assistant Director',
[Supervisor] AS 'Supervisor',
[Worker] AS 'Worker',
POSITION_NBR,
File_NBR
FROM
(SELECT * FROM org WHERE
(POSITION_NBR IN
(SELECT POSITION_NBR
FROM **#STAFF**
where Job_Category ='Worker'))
) AS p
PIVOT
(
MAX(Name) FOR
Job_Category IN ([Vice President],[Associate VP],
[Director],[Director B],[Assistant Director], [Supervisor], [Worker] )
) AS pvt