SQL pivot与unpivot 实现的简单的:行转列及列转行

 create table #tb(name varchar(32),kechen varchar(32),score float);
 
 insert into #tb values('小明','数学',99)
 insert into #tb values('小明','英语',60)
 insert into #tb values('小明','语文',100)
 insert into #tb values('小强','数学',29)
 insert into #tb values('小强','语文',130) 
 --行转列
 select * from #tb pivot(sum(score)for kechen in([数学],[英语],[语文]))a
 --列转行
 select * from #tb1 unpivot(score for kechen in ([数学],[英语],[语文])) a

 

posted @ 2013-04-04 22:25  csdnbbs  阅读(139)  评论(0)    收藏  举报