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的「訂單」,那麼您可以用該列替換order by null
中的null
。 order by created_at
這是正確的。如果我想爲每個實體ID更新001,002,那麼我應該怎麼做,以便當實體ID改變時,號碼再次重複001,002,003 ...? – ErrorNotFoundException
@Stanley:看我的編輯 –
謝謝,也工作。非常感謝你 – ErrorNotFoundException