SQL Server 2008 Application Series-Directory index
Explain the pivot and Unpivot examples to the new members today and sort out their usage by the way. This is a new feature provided from SQL Server 2005.
Official Example: http://msdn.microsoft.com/zh-cn/library/ms177410%28v=sql.105%29.aspx
First look at the pivot example:
Basic table Data:[SQL] View plain copy print? if not object_id (' tb_income ') IS NULL drop table [tb_Income] /****** object: table [dbo]. [tb_income] script date: 2012/4/5 8:19:21 ******/ CREATE TABLE [DBO]. [Tb_income] ( ----[Pkid] int primary key identity (101,1), [pname] [nvarchar] NOT NULL, [cyear] smallint NOT NULL, [CMonth] TinyInt NOT NULL, [CMoney] Decimal (10,2) Not Null ) go insert [dbo]. [tb_income] select ' hu Yi Dao ',2011,2,5600 union all select ' Hu Yi Dao ',2011,1,5678 union all select ' hu Yi Dao ',2011,3,6798 union all select ' Hu Yi Dao ',2011,4,7800 union all select ' Hu Yi Dao ', 2011,5,8899 union all select ' hu Yi Dao ',2011,8,8877 union ALL SELECT ' Hu Yi Dao ',2011,6,7788 union all select ' hu Yi Dao ',2011,7,6798 union ALL SELECT ' Hu Yi Dao ',2011,10,10000 union all select ' hu Yi Dao ',2011,9,12021 union all select ' Hu Yi Dao ',2011,11,8799 union all select ' Hu Yi Dao ', 2011,12,10002 union all select ' Miao ',2011,1,3455 union all select ' Miao ',2011,2,4567 union all select ' Miao ',2011,3,5676 union