想做一个年度累计帐,要求每一个年度开始重新累计。如下:请教高手存储过程如何来写。表A:
姓名     日期          总额
张三 2010-3-15         55
张三 2010-3-16         45
张三 2010-3-18         60
张三 2011-3-15         70
张三 2011-3-16         55
李四 2010-3-16         45
李四 2010-3-18         60
李四 2011-3-19         70
李四  2011-3-21        20
从表A中查询数据,把最后想得到的结果要插入到表B中,
表B:
名称     年度        日期            累计总额
张三     2010        2010-3-15        55
张三     2010        2010-3-16        100
李四     2010        2010-3-16        45
张三     2010        2010-3-18        160
李四     2010        2010-3-18        105
张三     2011        2011-3-15        70
张三     2011        2011-3-16        125 
李四     2011        2011-3-19        70
李四     2011        2011-3-21        90
每天从表A中查询数据,然后累加,也就是说如果今天是2010-3-16,从表A中查出所有的2010-3-16的数据,然后在表B中查出离2010-3-16
这个日期最近的那条数据,当然是要根据名称匹配的,然后把表A中查出来的总额和累计总额加起来,再插入到表B中。年度的作用是新的一年累计会从新开始

解决方案 »

  1.   

    -- 看一下:是不是下面这个逻辑:
    -- 如果今天是2011年3月16号,今天可能随时还会有业务发生,所以应该是今天统计并插入昨天的数据吧?
    create or replace procedure tbb_ins_proc
    is
      v_cnt1 number(18,0);
      v_cnt2 number(18,0);
    begin
      -- 查看tba表昨天是否发生了业务:
      select count(1) into v_cnt1 from tba where cdate=trunc(sysdate-1);
      -- 查看是否已将昨天的数据插入tbb表:
      select count(1) into v_cnt2 from tbb where cdate=trunc(sysdate-1);
      if v_cnt1>0 and v_cnt2>0 then -- 如果昨天发生了业务,并且昨天的数据没有插入统计表
      begin
        insert into ...
      end;
    end;
    /
      

  2.   

    create table tba(
      uname varchar2(30),
      cdate date,
      sums number(18,0)
    );alter table tba add constraints pk_tba primary key (uname,cdate);create table tbb(
      uname varchar2(30),
      years char(4),
      cdate date,
      all_sums number(18,0)
    );alter table tbb add constraints pk_tbb primary key (uname,years,cdate);insert into tba(uname,cdate,sums)
    select 
    '张三', to_date('2010-3-15','yyyy-mm-dd'), 55 from dual union all select
    '张三', to_date('2010-3-16','yyyy-mm-dd'), 45 from dual union all select
    '张三', to_date('2010-3-18','yyyy-mm-dd'), 60 from dual union all select
    '张三', to_date('2011-3-15','yyyy-mm-dd'), 70 from dual union all select
    '张三', to_date('2011-3-16','yyyy-mm-dd'), 55 from dual union all select
    '李四', to_date('2010-3-16','yyyy-mm-dd'), 45 from dual union all select
    '李四', to_date('2010-3-18','yyyy-mm-dd'), 60 from dual union all select
    '李四', to_date('2011-3-19','yyyy-mm-dd'), 70 from dual union all select
    '李四', to_date('2011-3-21','yyyy-mm-dd'), 20 from dual;commit;-- 整体插入语句如下:
    with a as(select t1.uname, to_char(t1.cdate,'yyyy') as year, trunc(t1.cdate) as cdate, sum(t1.sums) as sums
                from tba t1 group by t1.uname, to_char(t1.cdate,'yyyy'), trunc(t1.cdate) )
    select a1.uname, a1.year, a1.cdate, a1.sums, sum(a2.sums) as all_sums
    from a a1 left join a a2 on a1.uname=a2.uname and a1.cdate>=a2.cdate
    group by a1.uname, a1.year, a1.cdate, a1.sums;
      

  3.   

    用 rollup , grouping set
      

  4.   

    LZ看这个是你想要的吗?select 
    name,
    to_char(gdate,'yyyy'),
    max(gdate),
    sum(allm)
    from testa
    group by name,to_char(gdate,'yyyy');检索结果:san 2010 2010/3/18 160
    san 2011 2011/3/16 125
    si 2010 2010/3/18 105
    si 2011 2011/3/21 90