簡體   English   中英

按行數更新Oracle SQL

[英]Oracle SQL Update by rownum

我有以下格式的數據:

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的行數為止,就像我在上面填寫的那樣。

怎么做 ?

這是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
                     );

此版本假定item是相同的值不同ord_no

您可以為此使用合並語句

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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM