I have the following table structure:
create table abc ( Name number , No number )
The data is like this:
Name | No
-----+-----
100 | 11
101 | 20
102 | 12
103 | 16
104 | 13
105 | 14
106 | 15
I need to retain the exact values that are in the No
column; I just need to modify the records the current values are associated with, so that the seq
values are in the same order as the Name
values.
Name | No
-----+-----
100 | 11
101 | 12
102 | 13
103 | 14
104 | 15
105 | 16
106 | 20
Best Answer
I'm no pro on Oracle, but yo need something like this wrapped in an
UPDATE
statement,We look at the two columns
id
andseq
id
, we sort byid
, and return therow number()
andid
seq
, we sort byseq
, and return therow_number()
andseq
We have the same amount of rows for both, so we'll have the same amount of
row_number()
. Now we just join onrow_number()
. Tie-handling is undefined here.You should probably rename
seq
topriority
.priority
can be static and global depending on the context. The value can matter, as in a priority queue or job scheduler where some priorities hold significance.seq
by convention usually has gaps so the values don't matter, just the comparison (sometimes the uniqueness).Here is one attempt at making this approach work in Oracle (dbfiddle):
Referencing the same table three times probably won't lend itself well to efficiency, but this is something to start with at least.