我需要参考同一表上的另一个字段entity_id,使用顺序值(例如001、002、002)更新varchar2字段。我需要它,例如,如果我在两个不同的行上具有相同的entity_id,则顺序值应相同。
输出示例:
Entity_id Seq_field
1234 001
1234 001
4567 002
4567 002
3412 003
我尝试使用rownum,但是它为每个entity_id提供了不同的值,当然这些值没有尾随零。请帮忙。
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句