日期:2014-05-17 浏览次数:20522 次
select 姓名, max(case when 月份='一月' then 薪资 end) as [一月], max(case when 月份='二月' then 薪资 end) as [二月], max(case when 月份='三月' then 薪资 end) as [三月] from 表 group by 姓名
------解决方案--------------------
--行列转化 select 姓名, max(case when 月份='一月' then 薪资 end) as [一月], max(case when 月份='二月' then 薪资 end) as [二月], max(case when 月份='三月' then 薪资 end) as [三月], …… from 表 group by 姓名
------解决方案--------------------
declare @sql varchar(8000) set @sql = 'select 姓名 ' select @sql = @sql + ' , max(case 月份 when ''' + 月份 + ''' then 薪资 else 0 end) [' + 月份 + ']' from (select distinct 月份 from 表) as a set @sql = @sql + ' from 表 group by 姓名' exec sp_executesql @sql