请先看示意图:
这里表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的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分。不知道是不是要用到游标。
如果charge不为0的情况?
一样道理啦 无条件update
if exists(select 1 from table1 having sum(abs(CHARGE1))=0)
begininsert into table2
select ACC_NBR,CHARGE2,CHARGE3,CHARGE4,null
from table1end
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;
-------------------- ---------- ---------- ---------- ----------
111 3 56 23
222 34 64 23
333 6 4 7
那就是当charge的列的值为0的时候,左移,是要求用语句来判断的
不是我们人工判断。
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)
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>
--执行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)
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>
至于想用一条sql向新表插入符合要求的数据,我能想到的就是嵌套decode,繁琐的很。实在懒得写了,你可以自己试试。
总是有办法的,
找 一个 贴图空间,就可以了
我 找的是 www.photobucket.com