TSQL动态数据透视表

时间:2014-08-22 22:03:26

标签: sql-server tsql dynamic pivot

我正在使用像这样的select语句......

SELECT FileYear, FileSemester, StudentID, AssessmentCode, AssessAreaHdgAbbrev1, AssessResultsResult
FROM vStudentReportsSemesterResults
WHERE (FileYear = 2014) AND (FileSemester = 2) AND (assessmentCode LIKE '11%') AND (AssessAreaHdgAbbrev2 = 'Grade')

这给了我

FileYear  FileSemester   StudentID   assessmentCode   AssessAreaHdgAbbrev1  AssessResultsResult
-------------------------------------------------------------------------------------------    
2014        2             41965        11ENGADV        Ass1                       C
2014        2             41965        11ENGADV        Ass2                       C
2014        2             41965        11ENGADV        Ass3                       C
2014        2             41965        11ENGADV        Prelim                       
2014        2             41965        11HISANC        Ass1                       A
2014        2             41965        11HISANC        Ass2                       B
2014        2             41965        11HISANC        Ass3                       B
2014        2             41965        11HISANC        Prelim                       
2014        2             41965        11HISMOD        Ass1                       B
2014        2             41965        11HISMOD        Ass2                       B
2014        2             41965        11HISMOD        Ass3                       
2014        2             41965        11HISMOD        Prelim                       
2014        2             41965        11MATGEN        Ass1                       B
2014        2             41965        11MATGEN        Ass2                       
2014        2             41965        11MATGEN        Prelim                       
2014        2             41965        11PEDPDH        Ass1                       B
2014        2             41965        11PEDPDH        Ass2                       B
2014        2             41965        11PEDPDH        Ass3                       
2014        2             41965        11PEDPDH        Ass4                       
2014        2             41965        11PEDPDH        Prelim                       

我想动态地转动数据,给我一些看起来像

的东西
FileYear  FileSemester  StudentID  AssessmentCode  Ass1  Ass2  Ass3  Ass4  Prelim 
---------------------------------------------------------------------------------
2014        2            41965       11ENGADV        C    C    C        
2014        2            41965       11HISANC        A    B    B        
2014        2            41965       11HISMOD        B    B    
2014        2            41965       11MATGEN        B    
2014        2            41965       11PEDPDH        B    B    

请注意,它必须是动态的,因为评估点的数量可能会因主题和年份而异。

我已经查看了有关此内容的信息,并尝试将动态支点放在一起,如

DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)

select @ColumnName= ISNULL(@ColumnName + ',','') + QUOTENAME(AssessAreaHdgAbbrev1)
FROM (SELECT DISTINCT AssessAreaHdgAbbrev1 
FROM vStudentReportsSemesterResults
WHERE (FileYear = 2014) AND (FileSemester = 2) AND (AssessAreaHdgAbbrev2 = 'Grade') AND (AssessmentCode LIKE '11%')) AS Courses


SET @DynamicPivotQuery = N'
SELECT FileYear,  FileSemester, StudentID, AssessmentCode, AssessAreaHdgAbbrev1, AssessResultsResult
FROM vStudentReportsSemesterResults
WHERE (FileYear = 2014) AND (FileSemester = 2) AND (AssessAreaHdgAbbrev2 = ''grade'') AND (AssessmentCode LIKE ''11%'')
    PIVOT
    (
    MAX(AssessResultsResult) 
          FOR AssessAreaHdgAbbrev1 IN (' + @ColumnName + ')) AS PVTTable'

--Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery

我不断收到错误消息,例如

  

Msg 156,Level 15,State 1,Line 5
  关键字'PIVOT'附近的语法不正确。

我假设这是因为引号或where语句的语法。我有点沮丧,只是想知道是否有人可以查看我的代码并告诉我如何动态地转动数据,以便它在列中而不是在单独的行中提供等级。

非常感谢任何帮助。


更新

非常感谢你的帮助。

我的代码现在看起来像......

DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)

select @ColumnName= ISNULL(@ColumnName + ',','') + QUOTENAME(AssessAreaHdgAbbrev1)
FROM (SELECT DISTINCT AssessAreaHdgAbbrev1 
FROM vStudentReportsSemesterResults
WHERE (FileYear = 2014) AND (FileSemester = 2) AND (AssessAreaHdgAbbrev2 = 'Grade') AND (AssessmentCode LIKE '11%')) AS Courses

SET @DynamicPivotQuery = N'
SELECT 
FileYear,  
FileSemester, 
StudentID, 
AssessmentCode, 
AssessAreaHdgAbbrev1, 
AssessResultsResult
FROM (
SELECT * FROM vStudentReportsSemesterResults
WHERE (FileYear = 2014) 
    AND (FileSemester = 2) 
    AND (AssessAreaHdgAbbrev2 = ''grade'') 
    AND (AssessmentCode LIKE ''11%'')) src
PIVOT (MAX(AssessResultsResult) FOR AssessAreaHdgAbbrev1 IN (' + @ColumnName + ')) AS   PVTTable'

--Execute the Dynamic Pivot Query
EXEC sp_executesql @DynamicPivotQuery

当我跑步时,我得到了

Msg 207, Level 16, State 1, Line 7
Invalid column name 'AssessAreaHdgAbbrev1'.
Msg 207, Level 16, State 1, Line 8
Invalid column name 'AssessResultsResult'.

显然,由于某种原因,它无法识别这些字段。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

尝试:

SELECT StudentID, '+@ColumnName+'
FROM (
SELECT * FROM vStudentReportsSemesterResults
WHERE (FileYear = 2014) 
    AND (FileSemester = 2) 
    AND (AssessAreaHdgAbbrev2 = ''grade'') 
    AND (AssessmentCode LIKE ''11%'')) src
PIVOT (MAX(AssessResultsResult) FOR AssessAreaHdgAbbrev1 IN (' + @ColumnName + ')) AS PVTTable

一般:SELECT * FROM (SELECT foo) AS Foo PIVOT ...

查看PIVOT的文档:

USE AdventureWorks2008R2;

GO
SELECT VendorID, [250] AS Emp1, [251] AS Emp2, [256] AS Emp3, [257] AS Emp4, [260] AS Emp5
FROM 
(SELECT PurchaseOrderID, EmployeeID, VendorID
FROM Purchasing.PurchaseOrderHeader) AS p
PIVOT
(
COUNT (PurchaseOrderID)
FOR EmployeeID IN
( [250], [251], [256], [257], [260] )
) AS pvt
ORDER BY VendorID;