2007年1月份砂金入库金量
1月份 年累
一选厂 1.00 1.00
二选厂 2.00 2.00
三选厂 3.00 3.00
合计 6.00 6.002007年2月份砂金入库金量
2月份 年累
一选厂 4.00 5.00
二选厂 5.00 7.00
三选厂 6.00 9.00
合计 15.00 21.002007年3月份砂金入库金量
当月 年累
一选厂 7.00 12.00
二选厂 8.00 15.00
三选厂 9.00 18.00
合计 24.00 45.00
2007年4月份砂金入库金量
当月 年累
一选厂 10.00 22.00
二选厂 11.00 26.00
三选厂 12.00 30.00
合计 33.00 78.00
2007年5月份砂金入库金量
当月 年累
一选厂 13.00 35.00
二选厂 14.00 40.00
三选厂 15.00 45.00
合计 42.00 120.00
2007年6月份砂金入库金量
当月 年累
一选厂 16.00 51.00
二选厂 17.00 57.00
三选厂 18.00 63.00
合计 51.00 171.00
--------------------------------
如上面六个月的土所示,当我选择2007-1月的时候下面的表格里面就出现一月份的数据,我选择2007-2月的的时候将出现2月份的数据,请注意后面的年累,他是前面所有的月份的加上当前月份的!我选择2月时,年累也就是从1月份加到2月份,我选择4月份时候,就从1月累加到4月!
----
我在附件上面上传了个详细点的数据说明,和这个基本上差不多的!请 各位高手解答!谢谢!

解决方案 »

  1.   


    create table #t (入库时间 datetime , 供货单位 varchar(10), 原重g float)
    insert into #t values ('2007-1-1','一选厂',12 )
    insert into #t values ('2007-1-1','三选厂',20 )
    insert into #t values ('2007-1-1','二选厂',60 )
    insert into #t values ('2007-1-4','一选厂',21 )
    insert into #t values ('2007-2-1','三选厂',30 )
    insert into #t values ('2007-2-1','二选厂',10 )create proc wsp
    @date varchar(7)
    as
    select  供货单位,
    (select sum(原重g) from #t where 供货单位=a.供货单位 and datediff(mm,入库时间,cast(@date+'-1' as datetime))=0) '月累',
    (select sum(原重g) from #t where 供货单位=a.供货单位 and datepart(yy,入库时间)=left(@date,4))[年累]
    from #t a group by 供货单位--1月份的
    exec wsp '2007-1'--2月份的
    exec wsp '2007-2'
    ---依此类推。。
      

  2.   


    不好意思。漏了合计存储过程修改如下:create proc wsp
    @date varchar(7)
    as
    select  供货单位 ' ',
    (select sum(原重g) from #t where 供货单位=a.供货单位 and datediff(mm,入库时间,cast(@date+'-1' as datetime))=0) '月累',
    (select sum(原重g) from #t where 供货单位=a.供货单位 and datepart(yy,入库时间)=left(@date,4))[年累]
    from #t a group by 供货单位
    union all
    select  distinct '合计' ' ',(select sum(原重g) from #t where  datediff(mm,入库时间,cast(@date+'-1' as datetime))=0) '月累',
    (select sum(原重g) from #t where datepart(yy,入库时间)=left(@date,4))[年累]
    from #texec wsp '2007-1'
      

  3.   

    背着灵魂漫步 大哥!你这个样作出来的 是年累计的汇总不变啊 !他 是一年的汇总,我想要的是年累汇总是这样的!
    如果
    exec wsp '2007-4'的话,那么年累里面的数据是前4个月的就是1到4月总共的数据汇总,月累是当月的数据。
    例如下面的表结构:
    -------------------------------------------
    2007年1月份砂金入库金量 
           1月份  年累 
    一选厂 1.00  1.00 
    二选厂 2.00  2.00 
    三选厂 3.00  3.00 
    合计   6.00  6.00 
    -----------------------------------------
    2007年2月份砂金入库金量 
           2月份  年累 
    一选厂 4.00  5.00 
    二选厂 5.00  7.00 
    三选厂 6.00  9.00 
    合计   15.00 21.00 
    ----------------------------------------
    看上面2月份的年累是一月份的年累加上2月年的当月的数据 5=4+1
    -----------------------------------------
    2007年3月份砂金入库金量 
            当月   年累 
    一选厂 7.00  12.00 
    二选厂 8.00  15.00 
    三选厂 9.00  18.00 
    合计   24.00 45.00 
    ----------------------------------------
    三月份的年累等于二月份的年累加上当月的:12=5+7;
    也 就 是一月的年累加上二月的年累再加上当月的!12=1+4+7;以上列举的都是选厂1的!
    -------------------------------------------------------------------------------
      

  4.   


    哦。那改下:
    create proc wsp
    @date varchar(7)
    as
    select  供货单位 ' ',
    (select sum(原重g) from #t where 供货单位=a.供货单位 and datediff(mm,入库时间,cast(@date+'-1' as datetime))=0) '月累',
    (select sum(原重g) from #t where 供货单位=a.供货单位 and datepart(yy,入库时间)=left(@date,4)
    and datepart(mm,cast(@date+'-1' as datetime))>=datepart(mm,入库时间))[年累]
    from #t a group by 供货单位
    union all
    select  distinct '合计' ' ',(select sum(原重g) from #t where  datediff(mm,入库时间,cast(@date+'-1' as datetime))=0) '月累',
    (select sum(原重g) from #t where datepart(yy,入库时间)=left(@date,4)
    and datepart(mm,cast(@date+'-1' as datetime))>=datepart(mm,入库时间))[年累]
    from #texec wsp '2007-1'这样就可以了`
      

  5.   


    加上排序: alter proc wsp
    @date varchar(7)
    as
    select  供货单位 ' ',月累,年累 from
    (select 供货单位,
    (select sum(原重g) from #t where 供货单位=a.供货单位 and datediff(mm,入库时间,cast(@date+'-1' as datetime))=0) '月累',
    (select sum(原重g) from #t where 供货单位=a.供货单位 and datepart(yy,入库时间)=left(@date,4)
    and datepart(mm,cast(@date+'-1' as datetime))>=datepart(mm,入库时间))[年累]
    from #t a group by 供货单位
    union all
    select  distinct '合计' ' ',(select sum(原重g) from #t where  datediff(mm,入库时间,cast(@date+'-1' as datetime))=0) '月累',
    (select sum(原重g) from #t where datepart(yy,入库时间)=left(@date,4)
    and datepart(mm,cast(@date+'-1' as datetime))>=datepart(mm,入库时间))[年累]
    from #t)k
    order by replace(replace(replace(供货单位,'一',1),'二',2),'三',3)