用分析函数row_number
SQL> insert into test values ('2005-00001',1,'0001',to_date('2006-1-1','YYYY-MM-DD'));
1 row inserted
SQL> insert into test values ('2005-00001',2,'0001',to_date('2006-1-2','YYYY-MM-DD'));
1 row inserted
SQL> insert into test values ('2005-00001',3,'0001',to_date('2006-1-4','YYYY-MM-DD'));
1 row inserted
SQL> insert into test values ('2005-00001',4,'0002',to_date('2006-1-3','YYYY-MM-DD'));
1 row inserted
SQL> insert into test values ('2005-00002',1,'0001',to_date('2006-1-1','YYYY-MM-DD'));
1 row inserted
SQL> insert into test values ('2005-00002',2,'0001',to_date('2006-1-2','YYYY-MM-DD'));
1 row inserted
SQL> insert into test values ('2005-00002',3,'0001',to_date('2006-1-4','YYYY-MM-DD'));
1 row inserted
SQL> insert into test values ('2005-00002',4,'0002',to_date('2006-1-3','YYYY-MM-DD'));
1 row inserted
SQL> commit;
Commit complete
SQL>
SQL> update test t set t.xh=(
2 select tt.num from (select row_number() over (partition by pcode order by sj) as num,
3 t1.* from test t1)tt where tt.pcode=t.pcode and tt.areacode=t.areacode and tt.sj=t.sj)
4 ;
8 rows updated
SQL> commit;
Commit complete
SQL> select * from test;
PCODE XH AREACODE SJ
---------- ---------- -------- -----------
2005-00001 1 0001 2006-1-1
2005-00001 2 0001 2006-1-2
2005-00001 4 0001 2006-1-4
2005-00001 3 0002 2006-1-3
2005-00002 1 0001 2006-1-1
2005-00002 2 0001 2006-1-2
2005-00002 4 0001 2006-1-4
2005-00002 3 0002 2006-1-3
8 rows selected