本帖最后由 yubofighting 于 2011-10-11 10:18:54 编辑

解决方案 »

  1.   

    --try
    declare @sql varchar(max)
    set @sql=''
    select @sql=@sql+',max(case when month='''+month+''' then num else '''' end)['+month+']' from (select month from table)u
    order by [month]
    set @sql=stuff(@sql,1,1,'')
    set @sql='select id,'+@sql+' from tb_ot group by id'
    exec(@sql)
      

  2.   

    declare @sql varchar(max)
    set @sql=''
    select @sql=@sql+',max(case when month='''+month+''' then num else '''' end)['+month+']' from (select month from table)u
    order by [month]
    set @sql=stuff(@sql,1,1,'')
    set @sql='select id,'+@sql+' from tb_ot group by id'
    exec(@sql)