Pivot and UNPIVOT Usage

Source: Internet
Author: User


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 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.