比如:select cname,sum(qty) from cust where cdate between '2004/1/1' and '2004/2/1' group by cname现在需要加上汇总所有月份的sum(qty)列,是否有最简便的方法!

解决方案 »

  1.   

    select 分组字段=isnull(a.分组字段,b.分组字段),本月,所有月份累计
    from(
    select 分组字段,本月=sum(汇总字段)
    from 表
    where 月份=@月份
    group by 分组字段
    )a full join(
    select 分组字段,本月=sum(汇总字段)
    from 表
    where 月份<=@月份
    group by 分组字段
    )b on a.分组字段=b.分组字段
      

  2.   

    已调试成功,多谢老大,如下:
    select cpartcode=isnull(a.cpartcode,b.cpartcode),本月,所有月份
    from (select cpartcode,sum(ithsl) 本月 from _03jltz where dregistdate between '2004/3/1' and '2004/8/8' group by cpartcode ) a 
    full join(
    select cpartcode,sum(ithsl) 所有月份 from _03jltz where dregistdate<='2004/8/8' group by cpartcode) b
     on a.cpartcode=b.cpartcode
    这种句法真是很少见,还有那个full join ,在联机帮助中找不到,也许我太菜了,^_^