Give order to some elements for every user ORACLE

I have table consisting of notes for every user which looks like this:

user_id    note_id
-------    -------
      1       1232
      1       1246
      1       1223
      2       3342
      2       2134
      3       3212
      3       1243
    ...        ...

And now i like to implement an ordering mechanism which would allow user to arrange these notes in a user-specified way so that the final table would look something like this:

user_id    note_id  note_order
-------    -------  ----------
      1       1232          1
      1       1246          2
      1       1223          3
      2       3342          1
      2       2134          2
      3       3212          1
      3       1243          2
    ...        ...        ...

Of course after adding this column it has NULL values and I'm not allowed to create any more sequences in the database. Is there any way in ORACLE to achieve this goal?

I'd like the values in order column to start from 1 to the number of notes for every user and preferably do this in one update statement. If it`s impossible this way what would be the best construction for an PL/SQL loop or is there any other effective way to do this?

Looking forward to any possible help.

Answers


You don't need to store this value, you can simply generate this during retrieval.

select user_id,
       note_id,
       row_number() over (partition by user_id order by note_id)
from your_table

If you really need to update the table, you could do something like this:

merge into your_table ut
using (
  select rowid, 
         row_number() over (partition by user_id order by note_id) as rn
  from your_table
) t on (ut.rowid = t.rowid)
when matched then update
  set order_column = rn;

Note that you cannot create a column order because that is a reserved word (well actually you can by using double quotes, but I would strongly recommend not doing that).


Need Your Help

Django authentication and Ajax - URLs that require login

javascript python django authentication

I want to add some Ajax-niceness to my Django-coded website.

Reducing Build and Deploy time on a Android MapView app

android eclipse android-maps android-build

I am using Eclipse to build my application. If the application does not use Maps (MapView, MapActivity) then I can just hit Run, and it will build, deploy and launch the app on the phone. This takes