解决方案 »

  1.   

    try this,select 货物,规格,数量,单价,
                (select sum(金额) 
                 from [表名] b 
                 where b.货物=a.货物 and b.规格=a.规格 and datediff(mm,b.日期,getdate())=0) '本月金额',
                (select sum(金额) 
                 from [表名] b 
                 where b.货物=a.货物 and b.规格=a.规格 and datediff(mm,b.日期,getdate())=1) '上月金额'
     from [表名] a
     group by 货物,规格,数量,单价
      

  2.   


    declare @y int,@m int
    select @y=2013,@m=6  --> 指定当前月是2013年6月select 货物,规格,数量,单价,
                (select sum(金额) 
                 from [表名] b 
                 where b.货物=a.货物 and b.规格=a.规格 and datediff(mm,b.日期,cast(rtrim(@y)+'-'+rtrim(@m)+'-01' as datetime))=0) '本月金额',
                (select sum(金额) 
                 from [表名] b 
                 where b.货物=a.货物 and b.规格=a.规格 and datediff(mm,b.日期,cast(rtrim(@y)+'-'+rtrim(@m)+'-01' as datetime))=1) '上月金额'
     from [表名] a
     group by 货物,规格,数量,单价