如何用语句把例表1的奖金汇总排列?
例表1
员工编号 奖金 年 月
108 48.9 2006 1
108 18.3 2006 1
108 140 2006 1
110 10 2006 1
110 1470 2006 1
110 11 2006 1
110 33 2006 1
102 45 2006 1
102 2 2006 1
102 42.5 2006 1
108 42.5 2006 2
102 70 2006 2
108 45 2006 4
108 2 2006 4
110 70 2006 4
102 50 2006 4
102 1470 2006 4
例表2
员工编号 1月 2月 3月 4月
108 奖金金额 奖金金额 奖金金额 奖金金额
110 奖金金额 奖金金额 奖金金额 奖金金额
102 奖金金额 奖金金额 奖金金额 奖金金额
例表1
员工编号 奖金 年 月
108 48.9 2006 1
108 18.3 2006 1
108 140 2006 1
110 10 2006 1
110 1470 2006 1
110 11 2006 1
110 33 2006 1
102 45 2006 1
102 2 2006 1
102 42.5 2006 1
108 42.5 2006 2
102 70 2006 2
108 45 2006 4
108 2 2006 4
110 70 2006 4
102 50 2006 4
102 1470 2006 4
例表2
员工编号 1月 2月 3月 4月
108 奖金金额 奖金金额 奖金金额 奖金金额
110 奖金金额 奖金金额 奖金金额 奖金金额
102 奖金金额 奖金金额 奖金金额 奖金金额
insert ##Test
select 108, 48.9, 2006, 1 union all
select 108, 18.3, 2006, 1 union all
select 108, 140, 2006, 1 union all
select 110, 10, 2006, 1 union all
select 110, 1470, 2006, 1 union all
select 110, 11, 2006, 1 union all
select 110, 33, 2006, 1 union all
select 102, 45, 2006, 1 union all
select 102, 2, 2006, 1 union all
select 102, 42.5, 2006, 1 union all
select 108, 42.5, 2006, 2 union all
select 102, 70, 2006, 2 union all
select 108, 45, 2006, 4 union all
select 108, 2, 2006, 4 union all
select 110, 70, 2006, 4 union all
select 102, 50, 2006, 4 union all
select 102, 1470, 2006, 4declare @sql varchar(8000)
set @sql = 'select 员工编号'
select @sql = @sql + ', ' + '[' + cast(月 as varchar) + '月] = sum(case 月 when ' + cast(月 as varchar) + ' then 奖金 else 0 end)' from ##Test group by 月
set @sql = @sql + ' from ##Test where 年 = 2006 group by 员工编号'exec (@sql)
/*
员工编号 1月 2月 4月
102 89.5000 70.0000 1520.0000
108 207.2000 42.5000 47.0000
110 1524.0000 .0000 70.0000
*/drop table ##test
select 108, 48.9, 2006, 1
union all
select 108, 18.3, 2006, 1
union all
select 108, 140, 2006, 1
union all
select 110, 10, 2006, 1
union all
select 110, 1470, 2006, 1
union all
select 108, 42.5, 2006, 2
union all
select 102, 70, 2006, 2
union all
select 108, 45, 2006, 4select * from jj--第一种方法
select 员工编号,
[1月] = (select sum(case when 月=1 then 奖金 else 0 end) from jj where 员工编号 = a.员工编号),
[2月] = (select sum(case when 月=2 then 奖金 else 0 end) from jj where 员工编号 = a.员工编号),
[3月] = (select sum(case when 月=3 then 奖金 else 0 end) from jj where 员工编号 = a.员工编号),
[4月] = (select sum(case when 月=4 then 奖金 else 0 end) from jj where 员工编号 = a.员工编号)
from jj a
group by 员工编号 --第二种方法declare @sql nvarchar(4000)
set @sql = 'select 员工编号'select @sql = @sql + ',sum (case convert(nvarchar(1),[月]) when '''+ convert(nvarchar(1),月) +''' then 奖金 else 0 end ) as [' + convert(nvarchar(1),月) + '月]'
from jj group by 月select @sql = @sql + 'from jj group by 员工编号'exec(@sql)这样的行列转换的题太多啦,楼主应该先在网上找找,,到处都是.
create table #Test (员工编号 int, 奖金 money, 年 int, 月 int)
insert #Test
select 108, 48.9, 2006, 1 union all
select 108, 18.3, 2006, 1 union all
select 108, 140, 2006, 1 union all
select 110, 10, 2006, 1 union all
select 110, 1470, 2006, 1 union all
select 110, 11, 2006, 1 union all
select 110, 33, 2006, 1 union all
select 102, 45, 2006, 1 union all
select 102, 2, 2006, 1 union all
select 102, 42.5, 2006, 1 union all
select 108, 42.5, 2006, 2 union all
select 102, 70, 2006, 2 union all
select 108, 45, 2006, 4 union all
select 108, 2, 2006, 4 union all
select 110, 70, 2006, 4 union all
select 102, 50, 2006, 4 union all
select 102, 1470, 2006, 4-----------------------
declare @s varchar(8000)
select @s='select 员工编号'
select @s=@s+','+quotename(cast(月 as varchar))+'=sum(case 月 when '+ cast(月 as varchar)+' then 奖金 else 0 end) '
from #Test group by 月
select @s=@s+' from #Test group by 员工编号'
--print @s
exec(@s)
declare @s varchar(8000)
select @s='select 员工编号'
select @s=@s+','+quotename(cast(月 as varchar)+'月')+'=sum(case 月 when '+ cast(月 as varchar)+' then 奖金 else 0 end) '
from #Test group by 月
select @s=@s+' from #Test group by 员工编号'
--print @s
exec(@s)