Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 318
  • Last Modified:

SQL* Plus: Select rownums for each unique transaction

I have the table below in SQL*Plus: 8.0.6.0.0

drop table my_data;

create table my_data
(transaction                         NUMBER(6),
 my_product                        VARCHAR2(6),
 my_product_colour              VARCHAR2(35)
);

insert into my_data
values('12345','BAG','BLACK');
insert into my_data
values('12345','BAG','BLUE');
insert into my_data
values('12345','BAG','BLACK');
insert into my_data
values('12345','SHOES','RED');
insert into my_data
values('15896','BAG','BLACK');
insert into my_data
values('17845','BAG','BLACK');
insert into my_data
values('17845','SHOES','RED');

select * from my_data;

TRANSACTION MY_PRO   COLOUR
----------- ------ -----------------------------------
      12345       BAG         BLACK
      12345       BAG          BLUE
      12345       BAG          BLACK
      12345       SHOES         RED
      15896       BAG          BLACK
      17845       BAG          BLACK
      17845       SHOES          RED

7 rows selected.

What I want to do is select the data above but also with the line number
eg first product in the transaction to be number 1, then 2 etc,
then next transaction 1, 2 etc like below.

TRANSACTION MY_PRO COLOUR            my_row_nums      
----------- ------ -----------------------------------
      12345       BAG          BLACK                1
      12345       BAG          BLUE                                      2
      12345       BAG          BLACK                                    3
      12345       BAG          BLACK                                   4
      15896       BAG          BLACK            1
      17845       BAG         BLACK                                   1
      17845       SHOES          RED                                    2

Thanks,

Jason.
0
JasonAsh
Asked:
JasonAsh
1 Solution
 
sujith80Commented:
select TRANSACTION , MY_PROduct , my_product_COLOUR , row_number() over( partition by transaction order by MY_PROduct ) my_row_nums      
from my_data
/
0
 
JasonAshAuthor Commented:
Thanks Sujith80 that works perfect.

Thanks for the quick response,

Jason.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now