if
object_id(
'
tb
')
is
not
null
drop table tb
go
create table tb(
Name varchar( 10),
Course varchar( 10),
Score int
)
go
insert into tb values( ' 張三 ', ' 語文 ', 74)
insert into tb values( ' 張三 ', ' 數學 ', 83)
insert into tb values( ' 張三 ', ' 物理 ', 93)
insert into tb values( ' 李四 ', ' 語文 ', 74)
insert into tb values( ' 李四 ', ' 數學 ', 84)
insert into tb values( ' 李四 ', ' 物理 ', 94)
go
select * from tb
go
-- Old Way
select
Name,
MAX( case Course when ' 語文 ' then Score else 0 end) as 語文,
MAX( case Course when ' 數學 ' then Score else 0 end) as 數學,
MAX( case Course when ' 物理 ' then Score else 0 end) as 物理
from tb
group by Name
go
-- PIVOT
select * from tb
pivot(
max(Score) for Course in( [ 語文 ], [ 數學 ], [ 物理 ])
) pivotedTb
order by Name
go
-- UNPIVOT
select * from
(
select * from tb
pivot(
max(Score) for Course in( [ 語文 ], [ 數學 ], [ 物理 ])
) pivotedTb
) sourceTb
unpivot(
Score for Course in( [ 語文 ], [ 數學 ], [ 物理 ])
) unPivotedTb
drop table tb
go
create table tb(
Name varchar( 10),
Course varchar( 10),
Score int
)
go
insert into tb values( ' 張三 ', ' 語文 ', 74)
insert into tb values( ' 張三 ', ' 數學 ', 83)
insert into tb values( ' 張三 ', ' 物理 ', 93)
insert into tb values( ' 李四 ', ' 語文 ', 74)
insert into tb values( ' 李四 ', ' 數學 ', 84)
insert into tb values( ' 李四 ', ' 物理 ', 94)
go
select * from tb
go
-- Old Way
select
Name,
MAX( case Course when ' 語文 ' then Score else 0 end) as 語文,
MAX( case Course when ' 數學 ' then Score else 0 end) as 數學,
MAX( case Course when ' 物理 ' then Score else 0 end) as 物理
from tb
group by Name
go
-- PIVOT
select * from tb
pivot(
max(Score) for Course in( [ 語文 ], [ 數學 ], [ 物理 ])
) pivotedTb
order by Name
go
-- UNPIVOT
select * from
(
select * from tb
pivot(
max(Score) for Course in( [ 語文 ], [ 數學 ], [ 物理 ])
) pivotedTb
) sourceTb
unpivot(
Score for Course in( [ 語文 ], [ 數學 ], [ 物理 ])
) unPivotedTb
簡單演示了一下SQL Server2005 中的一對很有用的操作:PIVOT ,UNPIVOT
代碼如下:
if
object_id(
'
tb
')
is
not
null
drop table tb
go
create table tb(
Name varchar( 10),
Course varchar( 10),
Score int
)
go
insert into tb values( ' 張三 ', ' 語文 ', 74)
insert into tb values( ' 張三 ', ' 數學 ', 83)
insert into tb values( ' 張三 ', ' 物理 ', 93)
insert into tb values( ' 李四 ', ' 語文 ', 74)
insert into tb values( ' 李四 ', ' 數學 ', 84)
insert into tb values( ' 李四 ', ' 物理 ', 94)
go
select * from tb
go
drop table tb
go
create table tb(
Name varchar( 10),
Course varchar( 10),
Score int
)
go
insert into tb values( ' 張三 ', ' 語文 ', 74)
insert into tb values( ' 張三 ', ' 數學 ', 83)
insert into tb values( ' 張三 ', ' 物理 ', 93)
insert into tb values( ' 李四 ', ' 語文 ', 74)
insert into tb values( ' 李四 ', ' 數學 ', 84)
insert into tb values( ' 李四 ', ' 物理 ', 94)
go
select * from tb
go
--Old Wayselect
Name,
MAX( case Course when '語文' then Score else 0 end) as 語文,
MAX( case Course when '數學' then Score else 0 end) as 數學,
MAX( case Course when '物理' then Score else 0 end) as 物理
from tb
group by Name
go
--PIVOT
select * from tb
pivot(
max(Score) for Course in([語文],[數學],[物理])
) pivotedTb
order by Name
go
--UNPIVOT
select * from
(
select * from tb
pivot(
max(Score) for Course in([語文],[數學],[物理])
) pivotedTb
) sourceTb
unpivot(
Score for Course in([語文],[數學],[物理])
) unPivotedTb
go