我有以下格式的数据:
ORD_NO ITEM FULFILL_ID
SA1 1000 1
SA1 2000 2
SA2 2000 1
SA2 3000 2
SA2 9000 3我想填充列FULFILL_ID的值,它应该从1开始,一直增加到一个特定ORD NO的行数,就像我上面填充的那样。
该怎么做呢?
发布于 2015-09-28 20:01:01
这就是ROW_NUMBER()所做的:
select ord_no, item,
row_number() over (partition by ord_no order by item) as fulfill_id
from table t;这将以查询的形式返回值。更新需要一个稍有不同的查询。
编辑:
更新可以像这样完成:
update table t
set fulfill_id = (select count(*)
from table t2
where t2.ord_no = t.order_no and
t2.item <= t.item
);此版本假设ord_no的相同值的item是不同的。
发布于 2015-09-28 20:09:32
您可以使用merge语句来实现这一点
merge into table1 t3 using
(
select ord_no, item,
row_number() over (partition by ord_no order by item) as fulfill_id
from table1
) s
on
(t3.ORD_NO=s.ORD_NO and t3.ITEM=s.ITEM)
when matched then
update set t3.FULFILL_ID=s.fulfill_id https://stackoverflow.com/questions/32822427
复制相似问题