首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

请问游标处理过程中的有关问题-详见描述

2012-12-17 
请教游标处理过程中的问题-详见描述本帖最后由 hk1688 于 2012-11-05 12:47:17 编辑总体描述:数据表table_

请教游标处理过程中的问题-详见描述
本帖最后由 hk1688 于 2012-11-05 12:47:17 编辑 总体描述:
数据表table_a中有各个公司的数据,版本标识为0,如果是父节点的公司会有另一版本的数据,版本标识为1,我现在需要计算这两个版本的差异(新的公司公司数据:ID在原有几次公司ID前加9即可,版本标识为0),oracle11GR2,用游标处理。一下图解可以简单描述过程。

源数据表
公司ID(unit_id)版本标识(ver)数据值(dvalue)
1       1        260
1       0        45
11       0        80
12       1        90
12       0        50
121       0        20
122       0        30
13       0        60

想要结果
公司ID(unit_id)版本标识(ver)数据值(dvalue)
1        1        260
1        0        45
11        0        80
12        1        90
12        0        50
121        0        20
122        0        30
13        0        60
912        0        -10注:=90-(50+20+30)
91        0        -15注:=260-(45+80+50+20+30+60+(-10))

目前结果
公司ID(unit_id)版本标识(ver)数据值(dvalue)
1        1        260
1        0        45
11        0        80
12        1        90
12        0        50
121        0        20
122        0        30
13        0        60
912        0        -10
91        0        -25注:=260-(45+80+50+20+30+60)也就是说,没有把游标第一次算出来的数据"-10"算进去。

以下就是处理过程:

procedure proc1 is
  v_unit_id varchar2(100);
  --定义游标
  cursor c1 is
    select t.unit_id
    from table_a t
    where t.ver = 1
    order by length(t.unit_id) desc, t. unit_id desc; --获取1版本的unit_id
begin


  execute immediate 'TRUNCATE TABLE temp_table_a '; --使用临时表,保存差异数据
  open c1;
  loop
    fetch c1
      into v_unit_id;
    exit when c1%notfound;
    insert into temp_table_a
      select '9' || v_unit_id as unit_id, /* 此处加上9,标志差异数据公司的ID*/
             0 as ver,
             c.dvalue - b.dvalue as dvalue --差异数据
      from (select *
            from table_a t
            where t.unit_id = v_unit_id
            and t.ver = 1) c, --1版本的数据
           (select v_unit_id as unit_id, 0 as ver, a.value
            from (select sum(t.dvalue) as dvalue,
                  from table_a t
                  where t.unit_id like v_unit_id || '%'
                  and t.ver = 0) a) b --0版本的汇总数据
      where c.unit_id = b.unit_id;
    commit;
    /* 把该记录(抵消数据)插入ST源表,用以计算上一级的差异数据*/
    insert into table_a
      select *
      from temp_table_a t
      where t.unit_id not in (select a.unit_id from table_a a);
    commit;
  end loop;
  close c1;
end proc1



以上就是我的逻辑,按照我的逻辑,数据应该没有问题,或许是我的逻辑错了或者是我对游标理解错了,所以最终的数据错了,因此特向大家请教这个问题,非常感谢
[解决办法]
在线等,非常感谢
[解决办法]
没有人回答吗?或者本人QQ 99341799  非常感谢
[解决办法]
还没有吗?求解啊。

热点排行
Bad Request.