SQL Unpivot and Pivot

Source: Internet
Author: User
/*

table_source

PIVOT (

aggregate function (value_column) for pivot_column in

(<column_list>)

)

full syntax:

table_source

UNPIVOT (

value_column for

pivot_column in

(<column_list>)

)

*/

DECLARE @table table (name varchar (10), Course VARCHAR (10), fractional INT)

insert INTO @table VALUES (' Zhang San ', ' language ', ' the ')
INSERT into @table values (' Zhang San ', ' math ', Zhang San)
insert into @table values (' ', ' physical ', ' ")
insert into @table values (' John Doe ', ' language ', ', ')
insert into @table values (' John Doe ', ' math ', ' + ')
insert into @table values (' John Doe ', ' physical ', 94)


SELECT * From @table

SELECT * from  @table PIVOT (MAX (score) for course in (language, mathematics, physics)) a


DECLARE @table1 table (name VARCHAR (10 ), language int, math int, physical int)

INSERT into @table1 values (' Zhang San ', 74,83,93)

insert into @table1 values (' John Doe ', 74,84,94) C28/>select * from @table1
SELECT  Name,
        Lesson 1,
        score 1
from    @table1 UNPIVOT (score 1 for Course 1 in ([language ], [mathematics], [physical]) T 

  

SQL Unpivot and Pivot

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.