dbTalk Databases Forums  

renumbering the data in a column?

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss renumbering the data in a column? in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
mh@pixar.com
 
Posts: n/a

Default renumbering the data in a column? - 01-14-2009 , 02:02 PM






I have some rows that I would like to renumber. For example:

select * from t where name like 'x%' order by seq;

seq name
--- ----
15 xfoo
17 xbar
26 xbaz

desired state, seq column renumbered starting at 1:

seq name
--- ----
1 xfoo
2 xbar
3 xbaz

Is there a single sql statement that can do this? Or should I
just loop over the result set and manually update the seq
for each row?

Many TIA!
Mark

--
Mark Harrison
Pixar Animation Studios

Reply With Quote
  #2  
Old   
ddf
 
Posts: n/a

Default Re: renumbering the data in a column? - 01-14-2009 , 03:00 PM






On Jan 14, 2:02*pm, m... (AT) pixar (DOT) com wrote:
Quote:
I have some rows that I would like to renumber. *For example:

* * select * from t where name like 'x%' order by seq;

* * seq * *name
* * --- * *----
* * *15 * *xfoo
* * *17 * *xbar
* * *26 * *xbaz

desired state, seq column renumbered starting at 1:

* * seq * *name
* * --- * *----
* * * 1 * *xfoo
* * * 2 * *xbar
* * * 3 * *xbaz

Is there a single sql statement that can do this? *Or should I
just loop over the result set and manually update the seq
for each row?

Many TIA!
Mark

--
Mark Harrison
Pixar Animation Studios
This might get you started:

SQL> create table renum(
2 seq number,
3 name varchar2(10)
4 );

Table created.

SQL>
SQL> insert all
2 into renum
3 values(15,'xfoo')
4 into renum
5 values(17, 'xbar')
6 into renum
7 values(26, 'xbaz')
8 select * From dual;

3 rows created.

SQL>
SQL> commit;

Commit complete.

SQL>
SQL> select seq, name, rank() over (order by seq) rk
2 from renum;

SEQ NAME RK
---------- ---------- ----------
15 xfoo 1
17 xbar 2
26 xbaz 3

SQL>
SQL> update renum
2 set seq = (select num.rk from
3 (select seq, rank() over (order by seq) rk
4 from renum) num
5 where num.seq = renum.seq)
6 /

3 rows updated.

SQL>
SQL> select *
2 from renum;

SEQ NAME
---------- ----------
1 xfoo
2 xbar
3 xbaz

SQL>

You may need to modify the code for your actual data, but it,
hopefully, gives you a push in the right direction.


David Fitzjarrell


Reply With Quote
  #3  
Old   
Mark D Powell
 
Posts: n/a

Default Re: renumbering the data in a column? - 01-15-2009 , 08:15 AM



On Jan 14, 4:00*pm, ddf <orat... (AT) msn (DOT) com> wrote:
Quote:
On Jan 14, 2:02*pm, m... (AT) pixar (DOT) com wrote:





I have some rows that I would like to renumber. *For example:

* * select * from t where name like 'x%' order by seq;

* * seq * *name
* * --- * *----
* * *15 * *xfoo
* * *17 * *xbar
* * *26 * *xbaz

desired state, seq column renumbered starting at 1:

* * seq * *name
* * --- * *----
* * * 1 * *xfoo
* * * 2 * *xbar
* * * 3 * *xbaz

Is there a single sql statement that can do this? *Or should I
just loop over the result set and manually update the seq
for each row?

Many TIA!
Mark

--
Mark Harrison
Pixar Animation Studios

This might get you started:

SQL> create table renum(
* 2 * * * * *seq * * number,
* 3 * * * * *name * *varchar2(10)
* 4 *);

Table created.

SQL
SQL> insert all
* 2 *into renum
* 3 *values(15,'xfoo')
* 4 *into renum
* 5 *values(17, 'xbar')
* 6 *into renum
* 7 *values(26, 'xbaz')
* 8 *select * From dual;

3 rows created.

SQL
SQL> commit;

Commit complete.

SQL
SQL> select seq, name, rank() over (order by seq) rk
* 2 *from renum;

* * * *SEQ NAME * * * * * * * RK
---------- ---------- ----------
* * * * 15 xfoo * * * * * * * *1
* * * * 17 xbar * * * * * * * *2
* * * * 26 xbaz * * * * * * * *3

SQL
SQL> update renum
* 2 *set seq = (select num.rk from
* 3 * * * * * * * * *(select seq, rank() over (order by seq) rk
* 4 * * * * * * * * * * * * *from renum) num
* 5 * * * * * * * * *where num.seq = renum.seq)
* 6 */

3 rows updated.

SQL
SQL> select *
* 2 *from renum;

* * * *SEQ NAME
---------- ----------
* * * * *1 xfoo
* * * * *2 xbar
* * * * *3 xbaz

SQL

You may need to modify the code for your actual data, but it,
hopefully, gives you a push in the right direction.

David Fitzjarrell- Hide quoted text -

- Show quoted text -
Mark, David's plan is fine providing the sequence is not a PK or UK
referenced by a FK constraint, but why do you need to re-number the
sequence column? If the sequence field is a key then generated key
values are arbitrary in nature and gaps should not matter. The reason
I am asking has more to do with how the value will be handled going
forward rather than the 'fix' being made.

HTH -- Mark D Powell --



Reply With Quote
  #4  
Old   
gazzag
 
Posts: n/a

Default Re: renumbering the data in a column? - 01-15-2009 , 10:14 AM



On 14 Jan, 20:02, m... (AT) pixar (DOT) com wrote:
Quote:
I have some rows that I would like to renumber. *For example:

* * select * from t where name like 'x%' order by seq;

* * seq * *name
* * --- * *----
* * *15 * *xfoo
* * *17 * *xbar
* * *26 * *xbaz

desired state, seq column renumbered starting at 1:

* * seq * *name
* * --- * *----
* * * 1 * *xfoo
* * * 2 * *xbar
* * * 3 * *xbaz

Is there a single sql statement that can do this? *Or should I
just loop over the result set and manually update the seq
for each row?

Many TIA!
Mark

--
Mark Harrison
Pixar Animation Studios
I'm with Mark D Powell on this: why do you need to renumber that
column anyway?

-g


Reply With Quote
  #5  
Old   
mh@pixar.com
 
Posts: n/a

Default Re: renumbering the data in a column? - 01-15-2009 , 05:39 PM



Mark D Powell <Mark.Powell (AT) eds (DOT) com> wrote:
Quote:
On Jan 14, 4:00*pm, ddf <orat... (AT) msn (DOT) com> wrote:
You may need to modify the code for your actual data, but it,
hopefully, gives you a push in the right direction.
Thanks David, just what I needed!

Quote:
Mark, David's plan is fine providing the sequence is not a PK or UK
referenced by a FK constraint, but why do you need to re-number the
sequence column? If the sequence field is a key then generated key
values are arbitrary in nature and gaps should not matter. The reason
I am asking has more to do with how the value will be handled going
forward rather than the 'fix' being made.
Ah, I should have explained that part. It's part of our
film processing system, the seq is actually a queue position.
So it's an easy way to bump an emergency job to the front
of the queue.

It's a PK, but processed items are deleted from the system
so there's always a gap at the front..

Thanks!
Mark

--
Mark Harrison
Pixar Animation Studios



Reply With Quote
  #6  
Old   
Mark D Powell
 
Posts: n/a

Default Re: renumbering the data in a column? - 01-16-2009 , 11:52 AM



On Jan 15, 6:39*pm, m... (AT) pixar (DOT) com wrote:
Quote:
Mark D Powell <Mark.Pow... (AT) eds (DOT) com> wrote:

On Jan 14, 4:00*pm, ddf <orat... (AT) msn (DOT) com> wrote:
You may need to modify the code for your actual data, but it,
hopefully, gives you a push in the right direction.

Thanks David, just what I needed!

Mark, David's plan is fine providing the sequence is not a PK or UK
referenced by a FK constraint, but why do you need to re-number the
sequence column? *If the sequence field is a key then generated key
values are arbitrary in nature and gaps should not matter. *The reason
I am asking has more to do with how the value will be handled going
forward rather than the 'fix' being made.

Ah, I should have explained that part. *It's part of our
film processing system, the seq is actually a queue position.
So it's an easy way to bump an emergency job to the front
of the queue.

It's a PK, but processed items are deleted from the system
so there's always a gap at the front..

Thanks!
Mark

--
Mark Harrison
Pixar Animation Studios
Ok, but 15, 17, 26 became 1, 2, 3 so the relative order remained the
same. If the queue logic processes in key order what is the real need
to re-value the key? If you just want to show the relative queue
position 1..n then you can do that in the query results via row_number
or perhaps even using rownum of an ordered select, that is, select
film, rownum as QueuePosition from (select film from table order by
key)

If you need to change the processing order you could have the sequence
increment by 2 so that holes exist to move lower ranked items into
when you want to advance a lower ranked item.

Obviously there may be more to the process that your initial post and
minor followup explain so I just want to make sure you have considered
your options and are no doing unnecessary work.

HTH -- Mark D Powell --



Reply With Quote
  #7  
Old   
Robert Klemme
 
Posts: n/a

Default Re: renumbering the data in a column? - 01-17-2009 , 04:15 AM



On 16.01.2009 18:52, Mark D Powell wrote:
Quote:
On Jan 15, 6:39 pm, m... (AT) pixar (DOT) com wrote:
Mark D Powell <Mark.Pow... (AT) eds (DOT) com> wrote:

On Jan 14, 4:00 pm, ddf <orat... (AT) msn (DOT) com> wrote:
You may need to modify the code for your actual data, but it,
hopefully, gives you a push in the right direction.
Thanks David, just what I needed!

Mark, David's plan is fine providing the sequence is not a PK or UK
referenced by a FK constraint, but why do you need to re-number the
sequence column? If the sequence field is a key then generated key
values are arbitrary in nature and gaps should not matter. The reason
I am asking has more to do with how the value will be handled going
forward rather than the 'fix' being made.
Ah, I should have explained that part. It's part of our
film processing system, the seq is actually a queue position.
So it's an easy way to bump an emergency job to the front
of the queue.

It's a PK, but processed items are deleted from the system
so there's always a gap at the front..

Ok, but 15, 17, 26 became 1, 2, 3 so the relative order remained the
same. If the queue logic processes in key order what is the real need
to re-value the key? If you just want to show the relative queue
position 1..n then you can do that in the query results via row_number
or perhaps even using rownum of an ordered select, that is, select
film, rownum as QueuePosition from (select film from table order by
key)
Just guessing: maybe he is afraid of running out of sequence numbers or
wants to limit the sequence numbers because the fields has a small size
limit like NUMBER(4).

Quote:
If you need to change the processing order you could have the sequence
increment by 2 so that holes exist to move lower ranked items into
when you want to advance a lower ranked item.
Or use negative numbers for prepending.

Quote:
Obviously there may be more to the process that your initial post and
minor followup explain so I just want to make sure you have considered
your options and are no doing unnecessary work.
Another approach could be to use a TIMESTAMP (insertion timestamp by
default) for ordering. Then no Oracle sequence would be needed and also
no overflow could happen (in the next 100 years or so anyway). Downside
is of course that a TIMESTAMP needs more space in the DB and does not
guarantee uniqueness (i.e. two insertions in the same moment -
millisecond or whatever the chosen TIMESTAMP precision is).

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end


Reply With Quote
  #8  
Old   
joel garry
 
Posts: n/a

Default Re: renumbering the data in a column? - 01-19-2009 , 03:29 PM



On Jan 17, 2:15*am, Robert Klemme <shortcut... (AT) googlemail (DOT) com> wrote:
Quote:
On 16.01.2009 18:52, Mark D Powell wrote:





On Jan 15, 6:39 pm, m... (AT) pixar (DOT) com wrote:
Mark D Powell <Mark.Pow... (AT) eds (DOT) com> wrote:

On Jan 14, 4:00 pm, ddf <orat... (AT) msn (DOT) com> wrote:
You may need to modify the code for your actual data, but it,
hopefully, gives you a push in the right direction.
Thanks David, just what I needed!

Mark, David's plan is fine providing the sequence is not a PK or UK
referenced by a FK constraint, but why do you need to re-number the
sequence column? *If the sequence field is a key then generated key
values are arbitrary in nature and gaps should not matter. *The reason
I am asking has more to do with how the value will be handled going
forward rather than the 'fix' being made.
Ah, I should have explained that part. *It's part of our
film processing system, the seq is actually a queue position.
So it's an easy way to bump an emergency job to the front
of the queue.

It's a PK, but processed items are deleted from the system
so there's always a gap at the front..
Ok, but 15, 17, 26 became 1, 2, 3 so the relative order remained the
same. *If the queue logic processes in key order what is the real need
to re-value the key? *If you just want to show the relative queue
position 1..n then you can do that in the query results via row_number
or perhaps even using rownum of an ordered select, that is, select
film, rownum as QueuePosition from (select film from table order by
key)

Just guessing: maybe he is afraid of running out of sequence numbers or
wants to limit the sequence numbers because the fields has a small size
limit like NUMBER(4).
My guess is that it is to be human-readable - someone is going to be
looking at a list and saying "this one needs to go to the front."
What would you think of a top-ten list that had gaps in the numbers?

jg
--
@home.com is bogus.
http://catless.ncl.ac.uk/Risks/25.51.html#subj5


Reply With Quote
  #9  
Old   
Robert Klemme
 
Posts: n/a

Default Re: renumbering the data in a column? - 01-19-2009 , 03:58 PM



On 19.01.2009 22:29, joel garry wrote:
Quote:
On Jan 17, 2:15 am, Robert Klemme <shortcut... (AT) googlemail (DOT) com> wrote:
On 16.01.2009 18:52, Mark D Powell wrote:





On Jan 15, 6:39 pm, m... (AT) pixar (DOT) com wrote:
Mark D Powell <Mark.Pow... (AT) eds (DOT) com> wrote:
On Jan 14, 4:00 pm, ddf <orat... (AT) msn (DOT) com> wrote:
You may need to modify the code for your actual data, but it,
hopefully, gives you a push in the right direction.
Thanks David, just what I needed!
Mark, David's plan is fine providing the sequence is not a PK or UK
referenced by a FK constraint, but why do you need to re-number the
sequence column? If the sequence field is a key then generated key
values are arbitrary in nature and gaps should not matter. The reason
I am asking has more to do with how the value will be handled going
forward rather than the 'fix' being made.
Ah, I should have explained that part. It's part of our
film processing system, the seq is actually a queue position.
So it's an easy way to bump an emergency job to the front
of the queue.
It's a PK, but processed items are deleted from the system
so there's always a gap at the front..
Ok, but 15, 17, 26 became 1, 2, 3 so the relative order remained the
same. If the queue logic processes in key order what is the real need
to re-value the key? If you just want to show the relative queue
position 1..n then you can do that in the query results via row_number
or perhaps even using rownum of an ordered select, that is, select
film, rownum as QueuePosition from (select film from table order by
key)
Just guessing: maybe he is afraid of running out of sequence numbers or
wants to limit the sequence numbers because the fields has a small size
limit like NUMBER(4).

My guess is that it is to be human-readable - someone is going to be
looking at a list and saying "this one needs to go to the front."
What would you think of a top-ten list that had gaps in the numbers?
I'd think that in that case some numbers should repeat which is how
proper RANKing works. ;-)

And also, of course, you can easier count them if the numbers do not
have gaps.

Cheers

robert


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.