SQL Server 使用 PIVOT 和 UNPIVOT 实现多条行记录转置为列表
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
可以使用
语法以下语法总结了如何使用 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>; select <非透视的列>, [第一个透视的列] AS <列名称>, [第二个透视的列] AS <列名称>, ... [最后一个透视的列] AS <列名称>, from (<生成数据的 select 查询>) AS <源查询的别名> PIVOT ( <聚合函数>(<要聚合的列>) FOR [<包含要成为列标题的值的列>] IN ( [第一个透视的列], [第二个透视的列], ... [最后一个透视的列]) ) AS <透视表的别名> <可选的 ORDER BY 子句>; 备注
简单 PIVOT 示例下面的代码示例生成一个两列四行的表: USE AdventureWorks2014 ; 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; 下面是结果集: Cost_Sorted_By_Production_Days 0 1 2 3 4 ------------------------------ ----------- ----------- ----------- ----------- ----------- AverageCost 5.0885 223.88 359.1082 NULL 949.4105 复杂 PIVOT 示例若要生成交叉表报表来汇总数据,通常可能会发现 USE AdventureWorks2014; 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) p PIVOT ( COUNT (PurchaseOrderID) FOR EmployeeID IN ( [250], [251], [256], [257], [260] ) ) AS pvt ORDER BY pvt.VendorID; 以下为部分结果集: VendorID Emp1 Emp2 Emp3 Emp4 Emp5 ----------- ----------- ----------- ----------- ----------- ----------- 1492 2 5 4 4 4 1494 2 5 4 5 4 1496 2 4 4 5 5 1498 2 5 4 4 4 1500 3 4 4 5 4 将在 select PurchaseOrderID, EmployeeID, VendorID from PurchaseOrderHeader;
逆透视示例与 -- create the table and insert values as portrayed in the previous example. create TABLE pvt (VendorID INT, Emp1 INT, Emp2 INT, Emp3 INT, Emp4 INT, Emp5 INT); GO insert INTO pvt VALUES (1,4,3,5,4,4); insert INTO pvt VALUES (2,4,1,5,5,5); insert INTO pvt VALUES (3,4,3,5,4,4); insert INTO pvt VALUES (4,4,2,5,5,4); insert INTO pvt VALUES (5,5,1,5,5,5); GO -- Unpivot the table. select VendorID, Employee, Orders from (select VendorID, Emp1, Emp2, Emp3, Emp4, Emp5 from pvt) p UNPIVOT (Orders FOR Employee IN (Emp1, Emp2, Emp3, Emp4, Emp5) )AS unpvt; GO 以下为部分结果集: VendorID Employee Orders ----------- ----------- ------ 1 Emp1 4 1 Emp2 3 1 Emp3 5 1 Emp4 4 1 Emp5 4 2 Emp1 4 2 Emp2 1 2 Emp3 5 2 Emp4 5 2 Emp5 5 ... 请注意,
另请参阅from (Transact-SQL) 该文章在 2023/6/28 9:19:10 编辑过 |
关键字查询
相关文章
正在查询... |