merge into the_table
using
(
select rowid as rid,
entity_id,
to_char(dense_rank() over (order by entity_id), 'FM00000') as seq
from foo
) t on (the_table.rowid = t.rid)
when matched
then update set seq_field = t.seq;
如果要为每个序列开始一个新序列,entity_id
则需要稍微更改语句:
merge into foo
using
(
select rowid as rid,
entity_id,
to_char(row_number() over (partition by entity_id order by null), 'FM00000') as seq
from foo
) t on (foo.rowid = t.rid)
when matched
then update set seq_field = t.seq;
请注意,我使用row_number()
了dense_rank()
和partition by entity_id
的每个新值来重新开始编号entity_id
。如果您有另一列可以确定一个 entity_id 的“顺序”,那么您可以将null
in替换order by null
为该列,例如order by created_at