declare @sql nvarchar(max)
declare @sps_id nvarchar(50)
set @sps_id = ''
select distinct sps_date into #tb_group1 from ZZ_Spsdetail where sps_id = @sps_id order by sps_date--表头及排序
select @sql=ISNULL(@sql+',','')+'MAX(case sps_date when '''+sps_date+''' then sps_num else 0 end) ['+sps_date+']'
from #tb_group1
set @sql='select sps_id,sps_invcode,'+@sql+' from ZZ_Spsdetail a'+' where sps_id = ' + @sps_id +' group by sps_id,sps_invcode'
exec(@sql)
drop table #tb_group1