原文:sql pivot.unpivot和partition by用法 演示脚本 from sys.sysobjects where name = 'Student' AND type = 'U') BEGIN CREATE table Student( ID int identity primary key, ClassID , --班级ID CourseName ) , --课程 Name ) , --姓名 Score --成绩 ) END GO from Student) begin IN…
使用过SQL Server 2000的人都知道,要想实现行列转换,必须综合利用聚合函数和动态SQL,具体实现起来需要一定的技巧,而在SQL Server 2005中,使用新引进的关键字PIVOT/UNPIVOT,则可以很容易的实现行列转换的需求. 在本文中我们将通过两个简单的例子详细讲解PIVOT和UNPIVOT的用法. PIVOT是行转列,用法如下: 假如表结构如下: id name quarter profile 1 a 1 10…