2013-07-23 4 views
0

아래의 두 쿼리를 사용하여 뷰를 생성하려고하는데 오류 메시지가 나타납니다. 임시 테이블에 뷰 또는 함수가 허용되지 않습니다. 이렇게하는 방법이 있습니까? 아니면 아래의 두 쿼리를 결합하여 임시 테이블을 사용할 필요가 없습니까?두 개의 쿼리를 결합하기 위해 뷰에서 임시 테이블 사용

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 
+0

#staff를 채우는 데 사용하는 SQL 문을 가져 와서 하위 쿼리를 만들어 쿼리하는 두 테이블에 조인으로 추가 하시겠습니까? – Brian

답변

2

두 방법을 결합하는 가장 쉬운 방법은 첫 번째 쿼리를 두 번째 쿼리의 CTE로 이동하는 것입니다. 즉 다음과 같습니다

WITH staff AS(
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 
FROM New_EEs.dbo.vw_ADPFile 
), 
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 

그러나, #temptable 가능성이 성능을 향상시키기 위해 쿼리에 도입되었다. SQL Server에는 현재 뷰 내에 구체화 된 세트를 강제로 생성 할 수있는 방법이 없습니다. 따라서 동일한 쿼리를 작성할 수는 없습니다. 예를 들어 TOP(<large number>)ORDER BY을 추가하여 정렬과 같은 차단 연산자를 "직원"쿼리에 강제로 추가 할 수 있습니다. 이로 인해 SQL Server에서 임시 테이블을 사용하는 것과 동일한 스풀 연산자를 사용할 수 있습니다. 정렬의 추가 비용이 스풀로 얻는 이득보다 큽니다.

이렇게하면 다른 성능 튜닝 방법을 사용해야합니다. 적절한 인덱스가 있는지 확인하십시오. 통계가 최신입니다

관련 문제