在动态数据透视表中使用临时表

时间:2014-01-28 18:03:18

标签: sql-server pivot unpivot

我正在尝试在动态数据透视查询中使用临时表。我已经读过该过程不起作用,因为临时表在范围之外,即使它是一个全局表。这对我来说很奇怪,因为它适用于sql小提琴,但不适用于SQL Server 2012.如何编辑我的查询以便它为我提供除(xxx行受影响)以外的输出?

这是我的问题:

SELECT         cc.CaseCaseId AS CaseId, cc.Label AS CaseName, rce.EcoDate,  cc.OperatorName, cc.State, cc.County, ei.IROR,  rce.NDCash
into ##Temp2
FROM            PhdRpt.ReportCaseList_542 AS rcl INNER JOIN
                         CaseCases AS cc ON rcl.CaseCaseId = cc.CaseCaseId INNER JOIN
                         PhdRpt.RptCaseEco_542 AS rce ON rcl.ReportRunCaseId = rce.ReportRunCaseId INNER JOIN
                         PhdRpt.EcoIndicators_542 AS ei ON rcl.ReportRunCaseId = ei.ReportRunCaseId


DECLARE @colsPivot AS NVARCHAR(MAX),
 @colsUnpivot as NVARCHAR(MAX),
 @query AS NVARCHAR(MAX)

select @colsPivot = STUFF((SELECT distinct ',' + QUOTENAME(EcoDate) 
 from ##Temp2
 FOR XML PATH(''), TYPE
 ).value('.', 'NVARCHAR(MAX)') 
 ,1,1,'')

select @colsUnpivot = stuff((select ','+quotename(C.name)
 from sys.columns as C
 where C.object_id = object_id('##Temp2') and
 C.name LIKE 'NDCash%'
 for xml path('')), 1, 1, '')


set @query 
 = 'select *
 from
 (
 select cc.CaseCaseId AS CaseId, cc.Label AS CaseName, rce.EcoDate,  cc.OperatorName, cc.State, cc.County, ei.IROR, val, col
FROM          ##Temp2  
 unpivot
 (
 val
 for col in ('+ @colsunpivot +')
 ) u
 ) x1
 pivot
 (
 max(val)
 for EcoDate in ('+ @colspivot +')
 ) p'

exec(@query)


Drop table ##Temp2

这是表结构:

CaseId       EcoDate          NDCash
2         2003-01-01    26.6384943638238
2         2004-01-01    23.9534867373416
2         2005-01-01    25.9197356158675
2         2006-01-01    25.3280437702064
2         2007-01-01    24.9828609298022
2         2008-01-01    26.4019321789285
2         2009-01-01    26.6384943638238
2         2010-01-01    24.0660715481002
2         2011-01-01    26.6384943638238
2         2012-01-01    22.5718444448345
2         2013-01-01    26.6384943638238

2 个答案:

答案 0 :(得分:2)

除非我遗漏了某些内容,否则您无需使用UNPIVOT,也不需要使用临时表,只需直接查询数据即可。看起来您当前的查询只是忽略了一个不需要的列。您使用unpivot将多列转换为多行。

你应该可以使用:

DECLARE @colsPivot AS NVARCHAR(MAX),
 @query AS NVARCHAR(MAX)

select @colsPivot = STUFF((SELECT ',' + QUOTENAME(rce.EcoDate) 
                             from PhdRpt.RptCaseEco_542 AS rce
                             group by rce.EcoDate
                             order by rce.EcoDate
                     FOR XML PATH(''), TYPE
                     ).value('.', 'NVARCHAR(MAX)') 
                     ,1,1,'')

set @query 
 = 'select *
 from
 (
     SELECT cc.CaseCaseId AS CaseId, 
        cc.Label AS CaseName, 
        rce.EcoDate,  
        cc.OperatorName, 
        cc.State, 
        cc.County, 
        ei.IROR,  
        rce.NDCash
    FROM PhdRpt.ReportCaseList_542 AS rcl 
    INNER JOIN CaseCases AS cc 
        ON rcl.CaseCaseId = cc.CaseCaseId 
    INNER JOIN PhdRpt.RptCaseEco_542 AS rce 
        ON rcl.ReportRunCaseId = rce.ReportRunCaseId 
    INNER JOIN PhdRpt.EcoIndicators_542 AS ei 
        ON rcl.ReportRunCaseId = ei.ReportRunCaseId
 ) x1
 pivot
 (
     max(NDCash)
     for EcoDate in ('+ @colspivot +')
 ) p'

exec(@query)

答案 1 :(得分:0)

将exec(@query)更改为sp_executeSQl @query。

exec(@query)不会在当前上下文中执行,但是sp_executeSQL将在包含临时表的当前上下文中执行。