declare @sql varchar(8000)
set @sql = 'select 时间'
select @sql = @sql + ',sum(case 名称 when '''+名称+''' then 值 else 0 end) ['+名称+']'
from (select distinct 名称 from 有一表) as a
select @sql = @sql+' from 有一表 group by 时间'exec(@sql)
go
set @sql = 'select 时间'
select @sql = @sql + ',sum(case 名称 when '''+名称+''' then 值 else 0 end) ['+名称+']'
from (select distinct 名称 from 有一表) as a
select @sql = @sql+' from 有一表 group by 时间'exec(@sql)
go
set @sql = 'select 时间'
select @sql = @sql + ',sum(case 名称 when '''+名称+''' then 值 else 0 end) ['+名称+']'
from (select distinct 名称 from 有一表) as a
select @sql = @sql+' into 新表 from 有一表 group by 时间'exec(@sql)
select * from 新表
如果你要定期调用,那就要做成存储过程
create proc test
as
begin
if object_ID(新表) is not null
drop table 新表
declare @sql varchar(8000)
set @sql = 'select 时间'
select @sql = @sql + ',sum(case 名称 when '''+名称+''' then 值 else 0 end) ['+名称+']'
from (select distinct 名称 from 有一表) as a
select @sql = @sql+' into 新表 from 有一表 group by 时间'
exec(@sql)
select * from 新表
end