我有一个包含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
我想对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
怎么做?
答案 0 :(得分:5)
您可以使用row_number()
:
select cust_order,
row_number() over (partition by cust_order order by location) as fulfill_no,
item, location
from t;
实际上,在Oracle中更新数据可能会非常棘手。这是一种方式:
update t
set fulfill_no = (select count(*)
from t t2
where t2.cust_order = t.cust_order and t2.location <= t.location
);
答案 1 :(得分:0)
我想补充一下Gordon Linoff的回答。我想指出Gordon的row_number()解决方案适用于Microsoft SQL Server,Oracle和PostgreSQL。考虑到问题上的oracle标记,这个答案可能适用于提问者。
如果MySQL用户发现此问题并需要解决方案,则应使用会话变量。已经在stackoverflow上询问了在MySQL中实现row_number():ROW_NUMBER() in MySQL 对不起,我没有MySQL可用于测试,但是我可以尝试将上述查询自定义为MySQL ...
PhantomJS 2.1.1 (Linux 0.0.0) leave API service create(): should create a leave FAILED
static/app.min.js:4804:54
forEach@static/app.min.js:440:25
loadModules@static/app.min.js:4764:13
createInjector@static/app.min.js:4686:31
WorkFn@node_modules/angular-mocks/angular-mocks.js:3120:60
loaded@http://localhost:9876/context.js:151:17
TypeError: undefined is not an object (evaluating '$httpBackend.expectPOST') in test/leave.service.tests.js (line 65)
test/leave.service.tests.js:65:16
loaded@http://localhost:9876/context.js:151:17
有关更高级的用法,您可以尝试此链接。 http://www.mysqltutorial.org/mysql-row_number/