请先看示意图:
这里表1的charge1的值全部是0,我就想把这列的数据给去掉,相当于把数据往左移了。测试数据:
CREATE TABLE  table1 (
 ACC_NBR  varchar2 (20)   ,
 CHARGE1  varchar2 (10)   ,
 CHARGE2  varchar2 (10)   ,
 CHARGE3  varchar2 (10)   ,
 CHARGE4  varchar2 (10)   
)
insert into table1 (acc_nbr,charge1,charge2,charge3,charge4) values('111','0','3','56','23');
commit;
insert into table1 (acc_nbr,charge1,charge2,charge3,charge4) values('222','0','34','64','23');
commit;
insert into table1 (acc_nbr,charge1,charge2,charge3,charge4) values('333','0','6','4','7');
commit;为了方便,我把所有字段都设置成了varchar2 类型
table2的结构和table1一模一样
我想通过 insert 的方法,把table1的数据给插入到table2里,但是得达到示意图的效果。
请教各位了,这问题困扰了很久,能解决的给100分。不知道是不是要用到游标。

解决方案 »

  1.   

    这样子的需求描述不够详细,不知道有多少个charge?的列
    如果charge不为0的情况?
      

  2.   

    update table2 set charge1=charge2,charge2=charge3,charge3=charge4,charge4 ='';
      

  3.   

    写错 update table1 set charge1=charge2,charge2=charge3, charge3=charge4,charge4 ='';
    一样道理啦 无条件update
      

  4.   


    if exists(select 1 from table1 having sum(abs(CHARGE1))=0)
    begininsert into table2
    select ACC_NBR,CHARGE2,CHARGE3,CHARGE4,null
    from table1end
      

  5.   

    create table table2 as select ACC_NBR,charge2 a,charge3 b,charge4 c,' ' d from table1 where 1=1;
    alter table table2 rename column a to charge1;
    alter table table2 rename column b to charge2;
    alter table table2 rename column c to charge3;
    alter table table2 rename column d to charge4;
      

  6.   

    SQL> update table1 set CHARGE1=CHARGE2,CHARGE2=CHARGE3,CHARGE3=CHARGE4,CHARGE4='';已更新3行。SQL> select * from table1;ACC_NBR              CHARGE1    CHARGE2    CHARGE3    CHARGE4
    -------------------- ---------- ---------- ---------- ----------
    111                  3          56         23
    222                  34         64         23
    333                  6          4          7
      

  7.   

    看样子,有个重要的我没说明清楚
    那就是当charge的列的值为0的时候,左移,是要求用语句来判断的
    不是我们人工判断。
      

  8.   

    --执行4次
    UPDATE table1 
      SET charge1=decode(charge1,0,charge2,charge1),
          charge2=decode(charge2*charge2,0,charge3,charge2),
          charge3=decode(charge1*charge2*charge3,0,charge4,charge3),
          charge4=decode(charge1*charge2*charge3*charge4,0,0,charge4)
      

  9.   

    额,不用那么多,因为只有4个字段,执行3次就可以了,chargen执行n-1次
      

  10.   

    SQL> select * from table1
      2  ;
     
    ACC_NBR              CHARGE1    CHARGE2    CHARGE3    CHARGE4
    -------------------- ---------- ---------- ---------- ----------
    111                  3          56         23         0
    222                  34         64         23         0
    333                  6          4          7          0
     
    SQL> 
    SQL> UPDATE table1
      2    SET charge1=decode(charge1,0,charge2,charge1),
      3        charge2=decode(charge2*charge2,0,charge3,charge2),
      4        charge3=decode(charge1*charge2*charge3,0,charge4,charge3),
      5        charge4=decode(charge1*charge2*charge3*charge4,0,0,charge4)
      6  ;
     
    3 rows updated
     
    SQL> select * from table1;
     
    ACC_NBR              CHARGE1    CHARGE2    CHARGE3    CHARGE4
    -------------------- ---------- ---------- ---------- ----------
    111                  3          3          23         0
    222                  34         34         23         0
    333                  6          6          7          0
     
    SQL> 
      

  11.   

    倒,sql写错了,应该是charge1*charge2
    --执行4次
    UPDATE table1  
      SET charge1=decode(charge1,0,charge2,charge1),
      charge2=decode(charge1*charge2,0,charge3,charge2),
      charge3=decode(charge1*charge2*charge3,0,charge4,charge3),
      charge4=decode(charge1*charge2*charge3*charge4,0,0,charge4)
      

  12.   

    SQL> select * from table1;
     
    ACC_NBR              CHARGE1    CHARGE2    CHARGE3    CHARGE4
    -------------------- ---------- ---------- ---------- ----------
    111                  0          3          56         23
    222                  0          34         64         23
    333                  0          6          4          7
     
    SQL> 
    SQL> UPDATE table1
      2    SET charge1=decode(charge1,0,charge2,charge1),
      3    charge2=decode(charge1*charge2,0,charge3,charge2),
      4    charge3=decode(charge1*charge2*charge3,0,charge4,charge3),
      5    charge4=decode(charge1*charge2*charge3*charge4,0,0,charge4)
      6  ;
     
    3 rows updated
     
    SQL> select * from table1;
     
    ACC_NBR              CHARGE1    CHARGE2    CHARGE3    CHARGE4
    -------------------- ---------- ---------- ---------- ----------
    111                  3          56         23         0
    222                  34         64         23         0
    333                  6          4          7          0
     
    SQL> 
      

  13.   

    感谢你的方法,不过,因为考虑到我有一些实际应用的原因,需要使用插入到一个结构一模一样的空表中,先判断某一列的charge值是否为0,如果为0,则不插入该列值,不为0就插入。
      

  14.   

    呵呵,你可以先把这张表的数据insert到新表,然后对新表进行update。
    至于想用一条sql向新表插入符合要求的数据,我能想到的就是嵌套decode,繁琐的很。实在懒得写了,你可以自己试试。
    总是有办法的,
      

  15.   


    找 一个 贴图空间,就可以了
    我 找的是 www.photobucket.com