了解T-SQL中的PIVOTfunction

我对SQL很陌生。

我有这样一张桌子:

ID | TeamID | UserID | ElementID | PhaseID | Effort ----------------------------------------------------- 1 | 1 | 1 | 3 | 5 | 6.74 2 | 1 | 1 | 3 | 6 | 8.25 3 | 1 | 1 | 4 | 1 | 2.23 4 | 1 | 1 | 4 | 5 | 6.8 5 | 1 | 1 | 4 | 6 | 1.5 

我被告知要得到这样的数据

 ElementID | PhaseID1 | PhaseID5 | PhaseID6 -------------------------------------------- 3 | NULL | 6.74 | 8.25 4 | 2.23 | 6.8 | 1.5 

我知道我需要使用PIVOTfunction。 但不能清楚地理解。 如果有人可以在上面的情况下解释它,那将是很大的帮助(或者任何替代scheme)

用于将数据从一列转换为多列的PIVOT

对于你的例子,这里是一个STATIC Pivot,意思是你要对你想要旋转的列进行硬编码:

 create table temp ( id int, teamid int, userid int, elementid int, phaseid int, effort decimal(10, 5) ) insert into temp values (1,1,1,3,5,6.74) insert into temp values (2,1,1,3,6,8.25) insert into temp values (3,1,1,4,1,2.23) insert into temp values (4,1,1,4,5,6.8) insert into temp values (5,1,1,4,6,1.5) select elementid , [1] as phaseid1 , [5] as phaseid5 , [6] as phaseid6 from ( select elementid, phaseid, effort from temp ) x pivot ( max(effort) for phaseid in([1], [5], [6]) )p 

这是一个带有工作版本的SQL演示 。

这也可以通过一个dynamic的PIVOT来完成,您可以dynamic创build列表并执行PIVOT。

 DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX); select @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.phaseid) FROM temp c FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'') set @query = 'SELECT elementid, ' + @cols + ' from ( select elementid, phaseid, effort from temp ) x pivot ( max(effort) for phaseid in (' + @cols + ') ) p ' execute(@query) 

两者的结果:

 ELEMENTID PHASEID1 PHASEID5 PHASEID6 3 Null 6.74 8.25 4 2.23 6.8 1.5 

这些都是非常基本的主要例子。

SQL SERVER – PIVOT和UNPIVOT表的例子

产品表的链接示例:

 SELECT PRODUCT, FRED, KATE FROM ( SELECT CUST, PRODUCT, QTY FROM Product) up PIVOT (SUM(QTY) FOR CUST IN (FRED, KATE)) AS pvt ORDER BY PRODUCT 

呈现:

  PRODUCT FRED KATE -------------------- BEER 24 12 MILK 3 1 SODA NULL 6 VEG NULL 5 

类似的例子可以在SQL Server的博客post数据透视表中find。 一个简单的例子

设置兼容性错误

在使用pivot函数之前使用这个

 ALTER DATABASE [dbname] SET COMPATIBILITY_LEVEL = 100 

我是新来的这个,我创build一个不错的职位…我的问题是理解如何正确地应用聚合,这里是我的职位: http : //jaider.net/posts/1176-pivot-in-sql-server -correct聚集-结果/

在@bluefeet解决scheme中,必须提及的是, elementid是“无形” Group By的关键字段。 此外,您可以replaceelementid或添加更多的列,如userid

在这里输入图像描述

  SELECT <non-pivoted column>, [first pivoted column] AS <column name>, [second pivoted column] AS <column name>, ... [last pivoted column] AS <column name> FROM (<SELECT query that produces the data>) AS <alias for the source query> PIVOT ( <aggregation function>(<column being aggregated>) FOR [<column that contains the values that will become column headers>] IN ( [first pivoted column], [second pivoted column], ... [last pivoted column]) ) AS <alias for the pivot table> <optional ORDER BY clause>; USE AdventureWorks2008R2 ; GO SELECT DaysToManufacture, AVG(StandardCost) AS AverageCost FROM Production.Product GROUP BY DaysToManufacture; DaysToManufacture AverageCost 0 5.0885 1 223.88 2 359.1082 4 949.4105 -- Pivot table with one row and five columns SELECT 'AverageCost' AS Cost_Sorted_By_Production_Days, [0], [1], [2], [3], [4] FROM (SELECT DaysToManufacture, StandardCost FROM Production.Product) AS SourceTable PIVOT ( AVG(StandardCost) FOR DaysToManufacture IN ([0], [1], [2], [3], [4]) ) AS PivotTable; Here is the result set. Cost_Sorted_By_Production_Days 0 1 2 3 4 AverageCost 5.0885 223.88 359.1082 NULL 949.4105