SQL Server Pivot/unpivot rows and columns

Source: Internet
Author: User

Sometimes it comes down to the need for row-to-column (that is, the column's value as the column name), which I usually do with the case END + aggregate function.

As follows:

Declare @t table (studentname nvarchar), Subject nvarchar (+), score int) Insert into @t (Studentname,subject,score) Val    UEs (' Student a ', ' Chinese ', 80);    Insert into @t (studentname,subject,score) VALUES (' Student a ', ' math ', 78);    Insert into @t (studentname,subject,score) VALUES (' Student a ', ' English ', 92);    Insert into @t (studentname,subject,score) VALUES (' Student B ', ' Chinese ', 89);    Insert into @t (studentname,subject,score) VALUES (' Student B ', ' math ', 87);    Insert into @t (studentname,subject,score) VALUES (' Student B ', ' English ', 75);    Insert into @t (studentname,subject,score) VALUES (' Student C ', ' Chinese ', 92);    Insert into @t (studentname,subject,score) VALUES (' Student C ', ' math ', 74);    Insert into @t (studentname,subject,score) VALUES (' Student C ', ' English ', 65);    Insert into @t (studentname,subject,score) VALUES (' Student d ', ' Chinese ', 79);    Insert into @t (studentname,subject,score) VALUES (' Student d ', ' math ', 83);    Insert into @t (studentname,subject,score) VALUES (' Student d ', ' English ', 81); Insert into @t (studentname,subject,sCore) VALUES (' Student e ', ' Chinese ', 73);    Insert into @t (studentname,subject,score) VALUES (' Student e ', ' math ', 84);    Insert into @t (studentname,subject,score) VALUES (' Student e ', ' English ', 93);    Insert into @t (studentname,subject,score) VALUES (' Student f ', ' Chinese ', 79);    Insert into @t (studentname,subject,score) VALUES (' Student f ', ' math ', 86);   Insert into @t (studentname,subject,score) VALUES (' Student f ', ' English ', 84); Select Studentname, sum (case when Subject = N ' Chinese ' then score else 0 end) Chinese, sum (case when Subject = N ' Math ' Then score else 0 end] Math, sum (case when Subject = N ' English ' then score else 0 end) Engilsh from @t GROUP by Studen Tname

Seeing a new notation today,pivot can achieve the same functionality (2005 only starts to support).

The syntax for pivot is:

Table_source

Pivot (aggregate function (Value_column) pivot_column for (columnlist))

A little explanation:

Table_source: Is the table we want to convert. Pivot_column: Is the column name for row to column. Value_column: Is the value of the column after the conversion. Columnlist is the column to be generated.

This is also the case with pivot, which can be used to write the same result:

Select Studentname,       [Chinese] Chinese,       [math] math,       [English] 中文版 from  (SELECT * from @t) T1pivot (sum (score) for Subject in ([Chinese],[English],[mathematics]) T2

The corresponding Unpivot is the column change (column name as the value),

The syntax for Unpivot is:

Table_source

Unpivot (Value_column ubpivot_column for (columnlist))

The meaning of the parameter is the same as the pivot. Here we can simply turn back after the turn, so we get the original table:

Select Studentname,       Subject,       score from  (SELECT * from @t) T1pivot (sum (score) for Subject in ([English],[english],[math] )) T2unpivot (score for Subject in ([Chinese],[english],[math]) t3

SQL Server Pivot/unpivot rows and columns

Related Article

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.