很多情况下,需要对数据表中的数据由纵向变横向显示
也就是由较少的列根据一定的条件转化成较多的列显示。
下面的学生成绩表就是其中的一个例子:
/*
if object_id(’tempdb..#s’) is not null
drop table #s
create table #s(stu char(8),subject char(8),score int)
insert #s(stu,score,subject)
select ’小李’ , 88 , ’数学’
union all select ’小李’ , 77 , ’英语’
union all select ’小李’ , 55 , ’政治’
union all select ’小李’ , 30 , ’语文’
union all select ’小赵’ , 66 , ’数学’
union all select ’小赵’ , 23 , ’英语’
union all select ’小赵’ , 77 , ’政治’
union all select ’小赵’ , 87 , ’语文’
union all select ’小a’ , 22 , ’数学’
union all select ’小a’ , 74 , ’英语’
union all select ’小a’ , 99 , ’政治’
union all select ’小a’ , 23 , ’语文’