复制代码 代码如下:
select stdname,
isnull(sum( case stdsubject when ' 化学 ' then Result end), 0 ) [化学],
isnull(sum( case stdsubject when ' 数学 ' then Result end), 0 ) [数学],
isnull(sum( case stdsubject when ' 物理 ' then Result end), 0 ) [物理],
isnull(sum( case stdsubject when ' 语文 ' then Result end), 0 ) [语文]
from #student
group by stdname
复制代码 代码如下:
declare @sql varchar( 4000 )
set @sql = ' select stdname '
select @sql = @sql + ' ,isnull(sum(case stdsubject when ''' + stdsubject + ''' then Result end),0) [ ' + stdsubject + ' ] '
from (select distinct stdsubject from #student) as a
select @sql = @sql + ' from #student group by stdname '
print @sql
exec(@sql)
复制代码 代码如下:
select [name] into #tmpCloumns
from tempdb.dbo.syscolumns
where id = object_id( ' tempdb.dbo.#student2 ' )
and [name] <> ' stdname '
select * from #tmpCloumns
declare @strSql nvarchar( 800 )
select @strSql = ''
select @strSql = @strSql + ' union all ' + char ( 10 ) + char ( 13 ) +
' select [stdname], ''' + [name] + ''' as [科目],[ ' + [name] + ' ] ' + char ( 10 ) + char ( 13 ) +
' from [#student2] ' + char ( 10 ) + char ( 13 )
from #tmpCloumns
select @strSql = substring(@strSql, 11 ,len(@strSql)) + ' order by stdname,[科目] '
exec(@strsql)
新闻热点
疑难解答