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 
4

1 回答 1

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 中,目前无法强制在视图中创建具体化集。所以你不能写一个等效的查询。您可以尝试在“员工”查询中强制使用类似排序的阻塞运算符(例如,通过添加 aTOP(<large number>)和 an ORDER BY。这可能导致 SQL Server 使用相当于使用临时表的假脱机运算符。但是,很可能是适得其反,因为排序的额外成本将超过线轴所获得的收益。

这意味着,如果您要采用这种方式,则必须使用其他性能调整方法。确保您有适当的索引并且您的统计数据是最新的。

于 2013-07-23T14:16:59.563 回答