大家好,我需要一些帮助。
我必须使用pl / sql执行以下操作:
对于每个Request_number,在每个凭证上分配一个以1开头并递增1的数字的v_id。
例如,如果请求编号786530有3个与其关联的凭证,则第一个凭证应为v_id 1,第二个凭证应为v_id 2,第三个凭证应为v_id 3.
表格如下
Request_NO Voucher_no V_id
200 22211 null
200 22212 null
200 22213 null
201 22214 null
202 22215 null
202 22216 null
203 22217 null
204 22218 null
答案 0 :(得分:1)
您可以使用合并来提高效率:
create table vouchers
(request_no number not null
,voucher_no number primary key
,v_id number);
insert into vouchers values (200,22211,null);
insert into vouchers values (200,22212,null);
insert into vouchers values (200,22213,null);
insert into vouchers values (201,22214,null);
insert into vouchers values (202,22215,null);
insert into vouchers values (202,22216,null);
insert into vouchers values (203,22217,null);
insert into vouchers values (204,22218,null);
merge into vouchers t
using (select voucher_no
,row_number()
over (partition by request_no
order by 1)
as new_v_id
from vouchers) s
on (t.voucher_no = s.voucher_no)
when matched then update set v_id = new_v_id;
select * from vouchers;
req vouch v_id
=== ===== ====
200 22211 1
200 22212 2
200 22213 3
201 22214 1
202 22215 1
202 22216 2
203 22217 1
204 22218 1
答案 1 :(得分:0)
在Oracle中,您可以使用相关子查询执行此操作:
update table t
set v_id = (select count(*)
from table t2
where t2.Request_NO = t.Request_NO and
t2.Voucher_no <= t.Voucher_no
);
答案 2 :(得分:0)
update table t_ set v_id = (select dense_rank() over(partition by Request_NO,Voucher_no order by rownum) from t_);