在视图中使用临时表以组合两个查询

时间:2013-07-23 14:02:22

标签: sql-server views

我正在尝试使用以下两个查询创建一个视图,我收到错误消息临时表上不允许使用视图或函数。有没有办法做到这一点或有没有办法结合下面的两个查询,所以我不必使用临时表?

 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 

1 个答案:

答案 0 :(得分: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中,目前无法强制在视图中创建实现集。所以你不能写一个等价的查询。您可以尝试强制阻塞运算符(如排序)到“staff”查询中(例如,通过添加TOP(<large number>)ORDER BY。这可能导致SQL Server使用等效于使用临时表。然而,最有可能产生反效果,因为排序的额外成本将超过线轴所获得的收益。

这意味着,如果你这样做,你必须使用其他性能调整方法。确保您有适当的索引,并且您的统计信息是最新的。