dbTalk Databases Forums  

numbering a column according to group

comp.databases.oracle.server comp.databases.oracle.server


Discuss numbering a column according to group in the comp.databases.oracle.server forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Leigh
 
Posts: n/a

Default numbering a column according to group - 07-09-2003 , 11:35 PM






HELP! Anyone SQL gurus out there please help me with a query update to
a table:

I need to update a column in a table sequentially base on the date and
group by the customer number and the order item with data as such:

cust_no, order_item, order_date column_to_update
111 AAA 01/20/02 1
111 AAA 08/20/02 2
111 AAA 12/20/02 3
333 AAA 01/15/02 1
333 AAA 03/26/02 2
555 BBB 01/06/02 1
555 BBB 03/06/02 2
555 BBB 04/26/02 3
555 BBB 05/26/02 4

I only have oracle 8.1.7 Standard edition: so no analytical feature.

Thanks in advanced.

Reply With Quote
  #2  
Old   
Dave Hau
 
Posts: n/a

Default Re: numbering a column according to group - 07-10-2003 , 12:32 AM






"Leigh" <leighlhg (AT) hotmail (DOT) com> wrote

Quote:
HELP! Anyone SQL gurus out there please help me with a query update to
a table:

I need to update a column in a table sequentially base on the date and
group by the customer number and the order item with data as such:

cust_no, order_item, order_date column_to_update
111 AAA 01/20/02 1
111 AAA 08/20/02 2
111 AAA 12/20/02 3
333 AAA 01/15/02 1
333 AAA 03/26/02 2
555 BBB 01/06/02 1
555 BBB 03/06/02 2
555 BBB 04/26/02 3
555 BBB 05/26/02 4

I only have oracle 8.1.7 Standard edition: so no analytical feature.

Thanks in advanced.
Here's the sql for dense rank (i.e. rows with equal value receive the same
rank and the ranking number does not have any gap):

update your_table a
set column_to_update =
(select rownum from
(select order_date
from your_table b
group by b.cust_no, b.order_item, b.order_date
having b.cust_no = a.cust_no
and b.order_item = a.order_item
order by order_date) c
where c.order_date = a.order_date)


Here's the sql for non-dense rank (i.e. rows with equal value receive the
same rank but if two rows receive the same rank, the rank number will skip):

update your_table a
set column_to_update =
(select rownum from
(select order_date
from your_table b
group by b.cust_no, b.order_item
having b.cust_no = a.cust_no
and b.order_item = a.order_item
order by order_date) c
where c.order_date = a.order_date)


Cheers,
Dave




Reply With Quote
  #3  
Old   
Dave Hau
 
Posts: n/a

Default Re: numbering a column according to group - 07-10-2003 , 12:35 AM




"Dave Hau" <davehau-no-spam-123 (AT) no-spam (DOT) netscape.net> wrote

Quote:
"Leigh" <leighlhg (AT) hotmail (DOT) com> wrote in message
news:b8cf9ab1.0307092035.78ce99d8 (AT) posting (DOT) google.com...
HELP! Anyone SQL gurus out there please help me with a query update to
a table:

I need to update a column in a table sequentially base on the date and
group by the customer number and the order item with data as such:

cust_no, order_item, order_date column_to_update
111 AAA 01/20/02 1
111 AAA 08/20/02 2
111 AAA 12/20/02 3
333 AAA 01/15/02 1
333 AAA 03/26/02 2
555 BBB 01/06/02 1
555 BBB 03/06/02 2
555 BBB 04/26/02 3
555 BBB 05/26/02 4

I only have oracle 8.1.7 Standard edition: so no analytical feature.

Thanks in advanced.

Here's the sql for dense rank (i.e. rows with equal value receive the same
rank and the ranking number does not have any gap):

update your_table a
set column_to_update =
(select rownum from
(select order_date
from your_table b
group by b.cust_no, b.order_item, b.order_date
having b.cust_no = a.cust_no
and b.order_item = a.order_item
order by order_date) c
where c.order_date = a.order_date)


Here's the sql for non-dense rank (i.e. rows with equal value receive the
same rank but if two rows receive the same rank, the rank number will
skip):

update your_table a
set column_to_update =
(select rownum from
(select order_date
from your_table b
group by b.cust_no, b.order_item
having b.cust_no = a.cust_no
and b.order_item = a.order_item
order by order_date) c
where c.order_date = a.order_date)


Cheers,
Dave



Correction:

Here's the sql for dense rank (i.e. rows with equal value receive the same
rank and the ranking number does not have any gap):

update your_table a
set column_to_update =
(select rownum from
(select order_date
from your_table b
where b.cust_no = a.cust_no
and b.order_item = a.order_item
group by b.cust_no, b.order_item, b.order_date
order by b.order_date) c
where c.order_date = a.order_date)


Here's the sql for non-dense rank (i.e. rows with equal value receive the
same rank but if two rows receive the same rank, the rank number will skip):

update your_table a
set column_to_update =
(select rownum from
(select order_date
from your_table b
where b.cust_no = a.cust_no
and b.order_item = a.order_item
group by b.cust_no, b.order_item
order by b.order_date) c
where c.order_date = a.order_date)


Cheers,
Dave





Reply With Quote
  #4  
Old   
Dave Hau
 
Posts: n/a

Default Re: numbering a column according to group - 07-10-2003 , 12:42 AM




"Dave Hau" <davehau-no-spam-123 (AT) no-spam (DOT) netscape.net> wrote

Quote:
"Dave Hau" <davehau-no-spam-123 (AT) no-spam (DOT) netscape.net> wrote in message
news:GT6Pa.673$2u4.437 (AT) newssvr16 (DOT) news.prodigy.com...
"Leigh" <leighlhg (AT) hotmail (DOT) com> wrote in message
news:b8cf9ab1.0307092035.78ce99d8 (AT) posting (DOT) google.com...
HELP! Anyone SQL gurus out there please help me with a query update to
a table:

I need to update a column in a table sequentially base on the date and
group by the customer number and the order item with data as such:

cust_no, order_item, order_date column_to_update
111 AAA 01/20/02 1
111 AAA 08/20/02 2
111 AAA 12/20/02 3
333 AAA 01/15/02 1
333 AAA 03/26/02 2
555 BBB 01/06/02 1
555 BBB 03/06/02 2
555 BBB 04/26/02 3
555 BBB 05/26/02 4

I only have oracle 8.1.7 Standard edition: so no analytical feature.

Thanks in advanced.

Here's the sql for dense rank (i.e. rows with equal value receive the
same
rank and the ranking number does not have any gap):

update your_table a
set column_to_update =
(select rownum from
(select order_date
from your_table b
group by b.cust_no, b.order_item, b.order_date
having b.cust_no = a.cust_no
and b.order_item = a.order_item
order by order_date) c
where c.order_date = a.order_date)


Here's the sql for non-dense rank (i.e. rows with equal value receive
the
same rank but if two rows receive the same rank, the rank number will
skip):

update your_table a
set column_to_update =
(select rownum from
(select order_date
from your_table b
group by b.cust_no, b.order_item
having b.cust_no = a.cust_no
and b.order_item = a.order_item
order by order_date) c
where c.order_date = a.order_date)


Cheers,
Dave




Correction:

Here's the sql for dense rank (i.e. rows with equal value receive the same
rank and the ranking number does not have any gap):

update your_table a
set column_to_update =
(select rownum from
(select order_date
from your_table b
where b.cust_no = a.cust_no
and b.order_item = a.order_item
group by b.cust_no, b.order_item, b.order_date
order by b.order_date) c
where c.order_date = a.order_date)


Here's the sql for non-dense rank (i.e. rows with equal value receive the
same rank but if two rows receive the same rank, the rank number will
skip):

update your_table a
set column_to_update =
(select rownum from
(select order_date
from your_table b
where b.cust_no = a.cust_no
and b.order_item = a.order_item
group by b.cust_no, b.order_item
order by b.order_date) c
where c.order_date = a.order_date)


Cheers,
Dave



Correction for the case of non-dense rank:

Here's the sql for non-dense rank (i.e. rows with equal value receive the
same rank but if two rows receive the same rank, the rank number will skip):

update your_table a
set column_to_update =
(select min(rownum) from
(select order_date
from your_table b
where b.cust_no = a.cust_no
and b.order_item = a.order_item
order by b.order_date) c
where c.order_date = a.order_date)


Cheers,
Dave




Reply With Quote
  #5  
Old   
Leigh
 
Posts: n/a

Default Re: numbering a column according to group - 07-10-2003 , 03:03 PM



Thanks Dave,

I'll give them a try.

Leigh

"Dave Hau" <davehau-no-spam-123 (AT) no-spam (DOT) netscape.net> wrote

Quote:
"Dave Hau" <davehau-no-spam-123 (AT) no-spam (DOT) netscape.net> wrote in message
news:%W6Pa.675$Ic4.603 (AT) newssvr16 (DOT) news.prodigy.com...

"Dave Hau" <davehau-no-spam-123 (AT) no-spam (DOT) netscape.net> wrote in message
news:GT6Pa.673$2u4.437 (AT) newssvr16 (DOT) news.prodigy.com...
"Leigh" <leighlhg (AT) hotmail (DOT) com> wrote in message
news:b8cf9ab1.0307092035.78ce99d8 (AT) posting (DOT) google.com...
HELP! Anyone SQL gurus out there please help me with a query update to
a table:

I need to update a column in a table sequentially base on the date and
group by the customer number and the order item with data as such:

cust_no, order_item, order_date column_to_update
111 AAA 01/20/02 1
111 AAA 08/20/02 2
111 AAA 12/20/02 3
333 AAA 01/15/02 1
333 AAA 03/26/02 2
555 BBB 01/06/02 1
555 BBB 03/06/02 2
555 BBB 04/26/02 3
555 BBB 05/26/02 4

I only have oracle 8.1.7 Standard edition: so no analytical feature.

Thanks in advanced.

Here's the sql for dense rank (i.e. rows with equal value receive the
same
rank and the ranking number does not have any gap):

update your_table a
set column_to_update =
(select rownum from
(select order_date
from your_table b
group by b.cust_no, b.order_item, b.order_date
having b.cust_no = a.cust_no
and b.order_item = a.order_item
order by order_date) c
where c.order_date = a.order_date)


Here's the sql for non-dense rank (i.e. rows with equal value receive
the
same rank but if two rows receive the same rank, the rank number will
skip):

update your_table a
set column_to_update =
(select rownum from
(select order_date
from your_table b
group by b.cust_no, b.order_item
having b.cust_no = a.cust_no
and b.order_item = a.order_item
order by order_date) c
where c.order_date = a.order_date)


Cheers,
Dave




Correction:

Here's the sql for dense rank (i.e. rows with equal value receive the same
rank and the ranking number does not have any gap):

update your_table a
set column_to_update =
(select rownum from
(select order_date
from your_table b
where b.cust_no = a.cust_no
and b.order_item = a.order_item
group by b.cust_no, b.order_item, b.order_date
order by b.order_date) c
where c.order_date = a.order_date)


Here's the sql for non-dense rank (i.e. rows with equal value receive the
same rank but if two rows receive the same rank, the rank number will
skip):

update your_table a
set column_to_update =
(select rownum from
(select order_date
from your_table b
where b.cust_no = a.cust_no
and b.order_item = a.order_item
group by b.cust_no, b.order_item
order by b.order_date) c
where c.order_date = a.order_date)


Cheers,
Dave




Correction for the case of non-dense rank:

Here's the sql for non-dense rank (i.e. rows with equal value receive the
same rank but if two rows receive the same rank, the rank number will skip):

update your_table a
set column_to_update =
(select min(rownum) from
(select order_date
from your_table b
where b.cust_no = a.cust_no
and b.order_item = a.order_item
order by b.order_date) c
where c.order_date = a.order_date)


Cheers,
Dave

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.