SQL - 对表中的列数据进行排序

Imr*_*ani 0 sql oracle sequence

我有一个包含3个字段的表:

CUST_ORDER  FULFILL_NO  ITEM  LOCATION
   SA23        1        0233    11001
   SA23        1        0243    13001
   SA23        1        0513    14001
   SA88        1        0873    15001
   SA88        1        0533    17001
Run Code Online (Sandbox Code Playgroud)

我想对fulfill_no字段进行排序,以便数据变为:

CUST_ORDER  FULFILL_NO  ITEM  LOCATION
   SA23        1        0233    11001
   SA23        2        0243    13001
   SA23        3        0513    14001
   SA88        1        0873    15001
   SA88        2        0533    17001
Run Code Online (Sandbox Code Playgroud)

怎么做 ?

Gor*_*off 5

你可以使用row_number():

select cust_order,
       row_number() over (partition by cust_order order by location) as fulfill_no,
       item, location
from t;
Run Code Online (Sandbox Code Playgroud)

实际上,在Oracle中更新数据可能会非常棘手.这是一种方式:

update t
    set fulfill_no = (select count(*)
                      from t t2
                      where t2.cust_order = t.cust_order and t2.location <= t.location
                     );
Run Code Online (Sandbox Code Playgroud)