Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

reorder around defined positions

TPD-OpitzAug 8 2014 — edited Aug 8 2014

hello,

In my 11.2.0.3 oracle database I have a table that holds a "physical order" which is maintained by the system.

for some of the entries I have to recalculate a "logical order" based on the "physicalorder" but ignoring some entries in the table. No problem with that since row_number function is a friend of mine.

with my_data as (

  select 371 as PK,   2 as physical_index from dual union all

  select 381 as PK,   5 as physical_index from dual union all

  select 391 as PK,   9 as physical_index from dual union all

  select 401 as PK,   7 as physical_index from dual

  )

SELECT md.*

     , ROW_NUMBER( ) OVER (ORDER BY physical_index) AS logical_index

  FROM my_data md;

"pk","physical_index","logical_index"

371,  2,        1,

381,  5,        2,

391,  7,        3,

401,  9,        4,

This table is about to get another field "fixed position" where the user can enter a index at which this entry should rather be than what simple row_number() functon gives

with my_data as (

  select 371 as PK,   2 as physical_index,    3 as fixed_index from dual union all

  select 381 as PK,   5 as physical_index,    null as fixed_index from dual union all

  select 391 as PK,   7 as physical_index,    null as fixed_index from dual union all

  select 401 as PK,   9 as physical_index,    2 as fixed_index from dual

  )

SELECT md.*

     , null as logical_index -- what to write here?

  FROM my_data md;

should give

"pk","physical_index","fixed_index","logical_index"

371,   2,   3,   3,

381,   5,     ,   1,

391,   7,     ,   4,

401,   9,   2,   2,

is this possible with a single SQL?

bye

TPD

This post has been answered by BluShadow on Aug 8 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 5 2014
Added on Aug 8 2014
8 comments
244 views