/*
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