交叉表语句的实现:
--用于:交叉表的列数是确定的
select name,sum(case subject when ''''数学'''' then source else 0 end) as ''''数学'''',
sum(case subject when ''''英语'''' then source else 0 end) as ''''英语'''',
sum(case subject when ''''语文'''' then source else 0 end) as ''''语文''''
from test
group by name
--用于:交叉表的列数是不确定的
declare @sql varchar(8000)
set @sql = ''''select name,''''
select @sql = @sql + ''''sum(case subject when ''''''''''''+subject+''''''''''''
then source else 0 end) as ''''''''''''+subject+'''''''''''',''''
from (select distinct subject from test) as a
select @sql = left(@sql,len(@sql)-1) + '''' from test group by name''''
exec(@sql)
go
运行结果:
上一页 [1] [2]
Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved. 闵涛 E_mail:admin@mintao.net(欢迎提供学习资源)
鄂公网安备 42011102001154号
站长:MinTao ICP备案号:鄂ICP备11006601号-18