select [编号] ,[姓名] ,[部门],sum([2004_11月_基本工资]) as [2004_11月_基本工资]
from (
select [编号] ,[姓名] ,[部门],case 工资年度+工资月份 when '200411' then 基本工资 else 0 end as [2004_11月_基本工资] ,....(以下相同) from table ) table1
group [编号] ,[姓名] ,[部门]

解决方案 »

  1.   

    --大致如下,再将增幅换成%数就行了--示例--测试数据
    create table tb(工资年度 int,工资月份 int,编号 varchar(10),姓名 varchar(10),部门 varchar(10),基本工资 int,实发工资 int)
    insert tb select 2004,11,'A001','AAA','DDD',1000,1800
    union all select 2004,11,'B001','BBB','DDD',800 ,1500
    union all select 2004,12,'A001','AAA','DDD',1100,2000
    union all select 2004,12,'B001','BBB','DDD',1000,1600
    go--查询处理
    declare @s nvarchar(4000),@i int
    select @s='',@i=0
    select @s=@s+','+quotename(fd+'_基本工资')
    +'=sum(case when a.工资年度='+工资年度
    +' and a.工资月份='+工资月份
    +' then a.基本工资 end)'
    +case @i when 0 then ''
    else ','+quotename(fd+'_基本工资增幅')
    +'=sum(case when a.工资年度='+工资年度
    +' and a.工资月份='+工资月份
    +' then a.基本工资-b.基本工资 end)'
    end
    +','+quotename(fd+'_实发工资')
    +'=sum(case when a.工资年度='+工资年度
    +' and a.工资月份='+工资月份
    +' then a.实发工资 end)'
    +case @i when 0 then ''
    else ','+quotename(fd+'_实发工资增幅')
    +'=sum(case when a.工资年度='+工资年度
    +' and a.工资月份='+工资月份
    +' then a.实发工资-b.实发工资 end)'
    end,@i=@i+1
    from(
    select 
    工资年度=rtrim(工资年度),
    工资月份=rtrim(工资月份),
    fd=rtrim(工资年度)+'_'+rtrim(工资月份)
    from tb group by 工资年度,工资月份
    )a 
    exec('select a.编号,a.姓名,a.部门'+@s+'
    from tb a
    left join tb b on (a.工资年度=b.工资年度 and a.工资月份=b.工资月份)
    or (a.工资年度=b.工资年度-1 and a.工资月份=12 and b.工资月份=1)
    group by a.编号,a.姓名,a.部门')
    go
    --删除测试
    drop table tb/*--测试结果--*/
      

  2.   

    select 编号,姓名 ,部门,sum(2004_11月_基本工资) , sum(2004_12月_基本工资),2004年12月_基本工资增幅
     from tablename group by  [编号]
      

  3.   

    select a.*, (a.2004_12月_基本工资 - a.2004_11月_基本工资) '2004年12月_基本工资增幅',
    (a.2004_12月_实发工资 - a.2004_11月_实发工资) '2004年12月_实发工资增幅',
    (
    select max(编号) as 编号,max(姓名) as 姓名,max(部门) as 部门,
    '2004_11月_基本工资'=max(case when 工资年度=2004 and 工资月份=11 then 基本工资 else 0 end),
    '2004_12月_基本工资'=max(case when 工资年度=2004 and 工资月份=12 then 基本工资 else 0 end),
    '2004_11月_实发工资'=max(case when 工资年度=2004 and 工资月份=11 then 实发工资 else 0 end),
    '2004_12月_实发工资'=max(case when 工资年度=2004 and 工资月份=12 then 实发工资 else 0 end)
    from tbname 
    group by 编号,姓名,部门 ) a没有经过测试!你试一下。^O^
      

  4.   

    select  a.编号,a. 姓名 ,a. 部门,[2004_11月_基本工资]=b.基本工资,
    [2004_12月_基本工资]=a.基本工资,
    [2004年12月_基本工资增幅]=cast((1-cast(a.基本工资 as decimal(8,2))/b.a.基本工资)*100  as varchar(10))+'%',
    [2004_11月_实发工资]=b.实发工资, [2004_12月_实发工资] =a.实发工资,
    [2004年12月_实发工资增幅]=cast((1-cast(a.实发工资 as decimal(8,2))/b.a.实发工资)*100  as varchar(10))+'%'
      from (select  *  from   表 where 工资月份=12) a  left  join
    (select  *  from   表 where 工资月份=11)  b
    on   a.编号=b.编号  and a.姓名=b.姓名  and a.部门=b.部门
      

  5.   


    --大致如下,再将增幅换成%数就行了--示例--测试数据
    create table tb(工资年度 int,工资月份 int,编号 varchar(10),姓名 varchar(10),部门 varchar(10),基本工资 int,实发工资 int)
    insert tb select 2004,11,'A001','AAA','DDD',1000,1800
    union all select 2004,11,'B001','BBB','DDD',800 ,1500
    union all select 2004,12,'A001','AAA','DDD',1100,2000
    union all select 2004,12,'B001','BBB','DDD',1000,1600
    go--查询处理
    declare @s1 nvarchar(4000),@s2 nvarchar(4000),@i int
    select @s1='',@s2='',@i=0
    select @s1=@s1+','+quotename(fd+'_基本工资')
    +'=sum(case when a.工资年度='+工资年度
    +' and a.工资月份='+工资月份
    +' then a.基本工资 end)'
    +case @i when 0 then ''
    else ','+quotename(fd+'_基本工资增幅')
    +'=cast(cast(sum(case when a.工资年度='+工资年度
    +' and a.工资月份='+工资月份
    +' then a.基本工资-b.基本工资 end)*100.'
    +'/sum(case when a.工资年度='+工资年度
    +' and a.工资月份='+工资月份
    +' then a.基本工资 end)'
    +' as decimal(10,2)) as varchar)+''%'''
    end,
    @s2=@s2+','+quotename(fd+'_实发工资')
    +'=sum(case when a.工资年度='+工资年度
    +' and a.工资月份='+工资月份
    +' then a.实发工资 end)'
    +case @i when 0 then ''
    else ','+quotename(fd+'_实发工资增幅')
    +'=cast(cast(sum(case when a.工资年度='+工资年度
    +' and a.工资月份='+工资月份
    +' then a.实发工资-b.实发工资 end)*100.'
    +'/sum(case when a.工资年度='+工资年度
    +' and a.工资月份='+工资月份
    +' then a.实发工资 end)'
    +' as decimal(10,2)) as varchar)+''%'''
    end,
    @i=@i+1
    from(
    select 
    工资年度=rtrim(工资年度),
    工资月份=rtrim(工资月份),
    fd=rtrim(工资年度)+'_'+rtrim(工资月份)
    from tb group by 工资年度,工资月份)a 
    exec('select a.编号,a.姓名,a.部门'+@s1+@s2+'
    from tb a
    left join tb b on a.编号=b.编号 and a.姓名=b.姓名 and a.部门=b.部门
    and a.工资年度=b.工资年度+(b.工资月份)/12
    and a.工资月份=b.工资月份%12+1
    group by a.编号,a.姓名,a.部门')
    go
    --删除测试
    drop table tb/*--测试结果自己看--*/
      

  6.   

    --改一下,涨幅的基数写错了--示例--测试数据
    create table tb(工资年度 int,工资月份 int,编号 varchar(10),姓名 varchar(10),部门 varchar(10),基本工资 int,实发工资 int)
    insert tb select 2004,11,'A001','AAA','DDD',1000,1800
    union all select 2004,11,'B001','BBB','DDD',800 ,1500
    union all select 2004,12,'A001','AAA','DDD',1100,2000
    union all select 2004,12,'B001','BBB','DDD',1000,1600
    go--查询处理
    declare @s1 nvarchar(4000),@s2 nvarchar(4000),@i int
    select @s1='',@s2='',@i=0
    select @s1=@s1+','+quotename(fd+'_基本工资')
    +'=sum(case when a.工资年度='+工资年度
    +' and a.工资月份='+工资月份
    +' then a.基本工资 end)'
    +case @i when 0 then ''
    else ','+quotename(fd+'_基本工资增幅')
    +'=cast(cast(sum(case when a.工资年度='+工资年度
    +' and a.工资月份='+工资月份
    +' then a.基本工资-b.基本工资 end)*100.'
    +'/sum(case when a.工资年度='+工资年度
    +' and a.工资月份='+工资月份
    +' then b.基本工资 end)'
    +' as decimal(10,2)) as varchar)+''%'''
    end,
    @s2=@s2+','+quotename(fd+'_实发工资')
    +'=sum(case when a.工资年度='+工资年度
    +' and a.工资月份='+工资月份
    +' then a.实发工资 end)'
    +case @i when 0 then ''
    else ','+quotename(fd+'_实发工资增幅')
    +'=cast(cast(sum(case when a.工资年度='+工资年度
    +' and a.工资月份='+工资月份
    +' then a.实发工资-b.实发工资 end)*100.'
    +'/sum(case when a.工资年度='+工资年度
    +' and a.工资月份='+工资月份
    +' then b.实发工资 end)'
    +' as decimal(10,2)) as varchar)+''%'''
    end,
    @i=@i+1
    from(
    select 
    工资年度=rtrim(工资年度),
    工资月份=rtrim(工资月份),
    fd=rtrim(工资年度)+'_'+rtrim(工资月份)
    from tb group by 工资年度,工资月份)a 
    exec('select a.编号,a.姓名,a.部门'+@s1+@s2+'
    from tb a
    left join tb b on a.编号=b.编号 and a.姓名=b.姓名 and a.部门=b.部门
    and a.工资年度=b.工资年度+(b.工资月份)/12
    and a.工资月份=b.工资月份%12+1
    group by a.编号,a.姓名,a.部门')
    go
    --删除测试
    drop table tb/*--测试结果--*/
      

  7.   

    万分感谢各位大侠(特别是 zjcxc(邹建) 我现在就下去测试一下再说。