最近面试一大公司碰到一道题目:
原表:
SQL语句由列转行
要求查询的结果如下:
SQL语句由列转行

查询语句如下:
declare @sql varchar(8000)
set @sql = 'select name,'
select @sql = @sql + 'sum(case subject when '''+subject+''' 
then score else 0 end) as '''+subject+''','
from (select distinct subject from test_engine) as a
select @sql = left(@sql,len(@sql)-1) + ' from test_engine group by name'
print(@sql)
exec(@sql)

相关文章:

  • 2021-12-22
  • 2021-10-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-17
  • 2021-05-26
  • 2021-09-05
  • 2021-09-01
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案