如图所示,需要修改所有TRAIF_HS_CODE不为空的记录中TARIF_WTO的值
举个例子:比如TRAIF_HS_CODE为0201 10 000 1的记录,
它的TARIF_WTO的值要修改为
它本身的值+它下面TRAIF_HS_CODE为空的记录的TARIF_WTO的值
原值为:15 but not less
修改后的值:15 but not less than 0.2 euro per 1 kg所有TRAIF_HS_CODE不为空的记录都需要做这样的修改,
请问这样的SQL语句要怎么写呢?期待大家的指点...
UPDATE tab SET TARIF_WTO = (SELECT TARIF_WTO,ROWNUM WHERE tarif_hs_code IS NULL )
WHERE tarif_hs_code IS NOT NULL;
怀疑其实现可能性
不是把所有的tarif_hs_code IS NULL的记录中TARIF_WTO都加起来,只是把其下面的几条加起来
先按照空/非空做转换,空0非空1 nvl2(TRAIF_HS_CODE,1,0) a
然后对这列阶梯求和sum(a) over(order by rownum) b 这样分组就出来了
最后聚合wo_concat(TARIF_WTO) over(partition by b ) 就是你想要的查询结果如果要修改则update set 表 t set TARIF_WTO = (select ***** where 字段=t.字段)
就是一个SQL分组技巧而已,哪里扯出来这么多。
a varchar2(20);---a b c用来保存数据库中的TARIF_HS_CODE,TARIF_HS_CNAME,TARIF_WTO三个变量
b varchar2(300);
c varchar2(100);
k number(1);
begin
k := 0;
for eng in (select tarif_hs_code as a, tarif_hs_cname as b, tarif_wto as c
from tarif_middle) loop ---循环遍历表的记录
if (eng.a is not null and k = 1) then
insert into tarif_middle2
(tarif_hs_code, tarif_hs_cname, tarif_wto)
values
(a, b, c);
commit;
k := 0;
end if;
if (length(eng.a) = 13 and instr(eng.c, 'but not less', 1) = 0) then
a := eng.a;
b := eng.b;
c := eng.c;
k := 0;
insert into tarif_middle2
(tarif_hs_code, tarif_hs_cname, tarif_wto)
values
(a, b, c);
commit;
end if;
if (length(eng.a) = 13 and instr(eng.c, 'but not less', 1) > 0) then
a := eng.a;
b := eng.b;
c := eng.c;
k := 1;
end if;
if (eng.a is null and k = 1) then
c := c || ' '||eng.c;
end if;
end loop;
end meger;
并自动填充值要怎么实现呢?
注:sys_guid()是oracle生成guid的一个函数。