表名为salary (收入表)
字段:name varchar(20) (姓名)
    month int  (月份)
    income int  (收入)
我要查询出的结果集为:
姓名 当前月的收入 上个月的收入
谢谢大家赐教!
    

解决方案 »

  1.   

    declare @salary table (name   varchar(20),month   int,income   int )insert into @salary select '王',11,1200
    insert into @salary select '王',11,1300
    insert into @salary select '王',12,1400
    insert into @salary select '李',11,1500
    insert into @salary select '李',12,1600
    insert into @salary select '李',12,1700select distinct(name),
    当前月收入=(select  isnull(sum(income),0) from @salary where  name=a.name and [month]=month(getdate()) ),
    上月收入=(select isnull(sum(income),0) from @salary where name=a.name and  [month]=month(getdate())-1) 
    from @salary a李 3300 1500
    王 1400 2500