我对你的想法不是很清楚。你看一下这个能不能解决你的问题。不过这样做有个前提,就是把月份为空的月份值都改为0。这样可以加快速度啊。
create proc sp_sum
@mth integer
as
select [id] ,sum(金额) from pz where 月份<=@mth group by [id] 
我这么做是求这个供应商在小于等于你输入月份的金额和啊。不知道是你想要的吗?

解决方案 »

  1.   

    数据库是用友财务软件的,我不能更改数据,只能查
    你的语句只是产生了期末数,月份为null的数据是期初数,所有的金额加起来
    是期末数。
    我的要求是查询结果既要包含期初,又要包含期末,再具体就是要查出供应商
    的期初和期末。月份为null的数据是上年自动转入的,每个供应商只有一条,月份为数字是会计人员按发生平时一条一条输入的,同一供应商一个月有很多笔纪录
      

  2.   

    有好办法q我,我的qq29338361,注明csdn,我就会加你
      

  3.   

    我的做法是
    select venid,money from pz where riqi is null into #temptb1select venid,sum(money) as money from pz where month(riqi)<=@month or riqi is null
    group by venid into #temptb2select a.venid,a.money as qc,b.money as qm from #temptb1 a left join #temptb2 b
    on a.venid=b.venid
    很慢,不知为什么
      

  4.   

    哦,我知道你的意思了。可以这样做啊。
    create proc sp_sum
    @mth integer
    as
    select [id] ,sum(金额) as qm,sum(case when 月份 is null then 金额 else 0 end) as qc  from pz where 月份<=@mth group by [id] 
    试一下看看。